首页 > 编程知识 正文

求教大神这道java题整么做(java简单题)

时间:2023-12-12 16:20:11 阅读:314979 作者:GLPJ

本文目录一览:

这道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)到原点的距离的平方为%fn", p1.getX(), p1.getY(),

p1.distance());

Point p2 = new Point(1, 2);

System.out.printf("点(%f,%f)到点(%f,%f)的距离的平方为%fn", 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);

}

}

}

运行测试:

请点击输入图片描述

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。