本文目錄一覽:
這道Java題怎麼做?
首先定義Point類
public class Point {
private int x;
private int y;
private int z;
//無參構造
public Point(){}
//帶參數的構造函數,用於初始化坐標
public Point(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getZ() {
return z;
}
public void setZ(int z) {
this.z = z;
}
}
編寫Main方法,執行,驗證
import java.lang.Math;
public class Main {
public static double distancePOW(Point p1,Point p2)
{
return Math.pow(p1.getX()-p2.getX(),2)+Math.pow(p1.getY()-p2.getY(),2)+Math.pow(p1.getZ()-p2.getZ(),2);
}
public static void main(String[] args){
Point point1 = new Point(1,2,3);
Point point2 = new Point(5,0,0);
double distancePOW = distancePOW(point1,point2);
System.out.println(“兩個三維坐標點之間的距離的平方為”+distancePOW);
}
}
輸出結果:兩個三維坐標點之間的距離的平方為29.0
java這道題怎麼做?
// 建立一個汽車Auto類,包括輪胎個數,汽車顏色,車身重量、速度等成員變數。並通過不同的構造方法創建實例。
// 至少要求: 汽車能夠加速,減速,停車。再定義一個小汽車類Car,繼承Auto,並添加空調、CD等成員變數,覆蓋加速,減速的方法
class Auto {
public int 輪胎數量;
public String 汽車顏色;
public double 車身重量;
public double 速度;
Auto() {
// 無參構造
}
Auto(int 輪胎數量, String 汽車顏色, double 車身重量, double 速度) {
// 有參構造
this.輪胎數量 = 輪胎數量;
this.汽車顏色 = 汽車顏色;
this.車身重量 = 車身重量;
this.速度 = 速度;
}
public void 加速() {
System.out.println(“Auto加速”);
}
public void 減速() {
System.out.println(“Auto減速”);
}
public void 停車() {
System.out.println(“Auto停車”);
}
}
class Car extends Auto {
public String 空調;
public String CD;
public void 加速() {
System.out.println(“Car加速”);
}
public void 減速() {
System.out.println(“Car減速”);
}
}
這道java題應該怎麼做?
Java源代碼:
public class Test {
public static void main(String[] args) {
Point p1 = new Point(4, 5);
System.out.printf(“點p坐標為(%f,%f)\n”, p1.getX(), p1.getY());
p1.setX(3);
p1.setY(4);
System.out.printf(“重置後點p坐標為(%f,%f)\n”, p1.getX(), p1.getY());
System.out.printf(“點(%f, %f)到原點的距離的平方為%f\n”, p1.getX(), p1.getY(),
p1.distance());
Point p2 = new Point(1, 2);
System.out.printf(“點(%f,%f)到點(%f,%f)的距離的平方為%f\n”, p1.getX(),
p1.getY(), p2.getX(), p2.getY(), p1.distance(p2));
}
}
class Point {
protected double x;
protected double y;
public Point(){
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public void setX(double x) {
this.x = x;
}
public double getX() {
return this.x;
}
public void setY(double y) {
this.y = y;
}
public double getY() {
return this.y;
}
public double distance() {
return Math.pow(x, 2) + Math.pow(y, 2);
}
public double distance(Point p) {
return Math.pow(this.x – p.x, 2) + Math.pow(this.y – p.y, 2);
}
}
運行測試:
請問這個Java題怎麼做?
按照題目要求編寫的Java程序如下
import java.util.Scanner;
class RadiusException extends Exception{
public RadiusException(String message){
super(message);
}
}
public class Circle{
public double area(double r) throws RadiusException{
if(r0)
throw new RadiusException(“輸入錯誤”);
else
return Math.PI*r*r;
}
public static void main(String[] args){
System.out.print(“請輸入圓的半徑r:”);
try{
Scanner sc=new Scanner(System.in);
double r=sc.nextDouble();
Circle c=new Circle();
System.out.println(“圓的面積為”+c.area(r));
sc.close();
}catch(RadiusException re){
System.out.println(re.getMessage());
}
}
}
這道java題怎麼做?
Java參考源代碼:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Test10 extends JFrame implements ActionListener {
protected JList lstLeft = null;
protected JList lstRight = null;
protected JButton btnAdd = null;
protected String[] arr = {“新聞”, “娛樂”, “體育”, “教育”};
public Test10() {
super(“列表框”);
initComponent();
this.setSize(400, 300);
this.setVisible(true);
this.setLayout(new FlowLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void initComponent() {
lstLeft = new JList(arr);
lstRight = new JList();
btnAdd = new JButton(“”);
this.add(lstLeft);
this.add(btnAdd);
this.add(lstRight);
lstLeft.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
btnAdd.addActionListener(this);
}
public static void main(String[] args) {
new Test10();
}
@Override
public void actionPerformed(ActionEvent e) {
Object[] items = lstLeft.getSelectedValues();
DefaultListModel model = new DefaultListModel();
lstRight.setModel(model);
model.removeAllElements();
for(Object value : items) {
model.addElement(value);
}
}
}
運行測試:
請點擊輸入圖片描述
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/130161.html