类的定义试题
-
定义一个Rectangle类表示矩形。
其中含有length、width这两个double型的private成员变量表示矩形的长和宽。
定义一个无参数的构造方法Rectangle( ), 定义一个有参数的构造方法Rectangle (double length, double width )。
为length和width定义访问方法和修改方法。
定义求矩形周长的方法getPerimeter( ),求矩形面积方法getArea( )。
编写程序测试矩形类的所有方法。
public class Rectangle {
double length;
double width;
public Rectangle(double length,double width) {
this.length = length;
this.width = width;
}
public Rectangle() {
this(1.0,1.0);
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getPerimeter() {
return 2*(length+width);
}
public double getArea() {
return length*width;
}
}
public class RectText {public static void main(String[] args) {
Rectangle rect = new Rectangle();
rect.setLength(20);
rect.setWidth(20);
System.out.println(“该矩形的长为:” +rect.getLength());
System.out.println(“该矩形的宽为:” +rect.getWidth());
System.out.println(“该矩形的周长为:” +rect.getPerimeter());
System.out.println(“该矩形的面积为:” +rect.getArea());}
-
定义一个Point类,表示平面坐标的一个点。
数据域x和y分别表示它们的坐标。
一个创建点(0.0, 0.0)的无参构造方法。一个创建特定坐标点的构造方法。
一个名为distance的方法,返回从该点到Point类型的指定点之间的距离。另一个名为distance的方法,返回从该点到原点之间的距离。
覆盖Object类的toString( )方法。字符串格式: (x,y)
编写一个测试类PointTest,创建两个点(0.0,0.0)和(10.0,30.5),并输出两点之间的距离。
public class Point {
private double x;
private double y;
public Point() {}
public Point(double x,double y) {
this.x = x;
this.y = y;} public double distance(Point p) { return Math.sqrt((this.x-p.x)*(this.x-p.x)+(this.y-p.y)*(this.y-p.y)); } public double distance() { return Math.sqrt(x*x+y*y);
}
}
public class PointText {public static void main(String[] args) {
Point p1 = new Point();
Point p2 = new Point(10.0,30.5);
System.out.println(p1.distance(p2));
System.out.println(p2.distance());}
}
3) 定义一个复数类Complex,使得以下代码可以工作。
Complex c1=new Complex(3.0,5.0); //用复数3+5i初始化c1
Complex c2=new Complex(4.5); //用实数4.5初始化c2
c1.add(c2); //将c1与c2相加,结果保存在c1中
c1.show(); //将c1输出
public class Complex {
double realPart;
double imaginPart;
public Complex(double r,double i) {
realPart = r;
imaginPart = i;
}
public Complex(double r) {
realPart = r;
}
public Complex add(Complex c) {
this.realPart += c.realPart;
this.imaginPart += c.imaginPart;
return this;
}
public void show() {
System.out.println(realPart + “+” + imaginPart + “i”);
}
}
public class ComplexText {
public static void main(String[] args) {
Complex c1 = new Complex(3.0,5.0);
Complex c2 = new Complex(4.5);
c1.add(c2);
c1.show();
}
}
4) 定义一个名为NumberUtil类。
在该类中定义如下两个静态方法:
public static boolean isPrime( int n) // 返回n是否为素数
public static boolean isPalindrome( int n)
// 返回n是否为回文数,如363是回文数
在该类中编写main( )方法,求出1~1000的所有回文素数。
public class NumberUtil {
public static boolean isPrime(int n) {
for(int divisor = 2;divisor*divisor<=n;divisor++) {
if(n % divisor == 0)
return false;
}
return true;
}
public static boolean isPalindrome(int n) {
String s = String.valueOf(n);
int low = 0;
int height = s.length()-1;
while(low<height) {
if(s.charAt(low)!=s.charAt(height)) {
return false;
}
low++;
height–;
}
return true;
}
public static void main(String[] args) {
System.out.println("求出1~1000的所有回文素数: ");
int count = 0;
int n = 2;
do {
if(isPrime(n)&&isPalindrome(n)) {
count++;
if(count%10==0) {
System.out.println(n);
}else {
System.out.print(n + " ");
}
}
n++;
}while(count<20);
}
}
5) 定义一个名为Person的类。
在该类中有两个成员变量: String name 和 int age。
分别为这两个变量定义get方法和set方法。
为该类定义一个speak( )方法,输出其name和age的值。
编写程序,在主方法中测试get方法、set方法、speak方法。
public class Person {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void speak() {
System.out.println(“姓名:” + name);
System.out.println(“年龄:” + age);
}
}
public class PersonText {
public static void main(String[] args) {
Person p = new Person();
p.setName("朱骐");
p.setAge(19);
p.speak();
System.out.println(p.getName());
System.out.println(p.getAge());
}
}