JAVA--面向对象编程题

一、static关键字:

package Static;

public class Dome {
 public static void main(String [] args){
  //StackOverflowError栈溢出
  A a = new A();
  System.out.println(a ==a .a);
  System.out.println(a.a == a.a.a);
 }
}
class A{
 //int [][] arr= new int [1024][1024];
 //Exception in thread "main" java.lang.OutOfMemoryError: Java heap space堆内存溢出
 //A a = new A();
 //显示初始化
 static A a = new A();
}
/*
Exception in thread "main" java.lang.StackOverflowError
	at Static.A.<init>(Dome.java:12)
Exception in thread "main" java.lang.OutOfMemoryError
俩个溢出问题:没加static是有区别的。
 */

二.
1.俩个名为width 和height 的double型数据域,他们分别表示矩形宽和高,width和height默认值都是1
2.创建默认矩形的无参构造方法
3.一个创建width 和 height 为指定值得矩形构造方法
4.一个名为getArea()的方法返回矩形的面积
5.一个名为hetperimeter()的方法返回周长

package Static;
/*
1.俩个名为width 和height 的double型数据域,他们分别表示矩形宽和高,width和height默认值都是1
2.创建默认矩形的无参构造方法
3.一个创建width 和 height 为指定值得矩形构造方法
4.一个名为getArea()的方法返回矩形的面积
5.一个名为hetperimeter()的方法返回周长
 */
public class Demo106 {
 public static void main(String [] args){
Rectangle r1 = new Rectangle();
System.out.println(r1.getArea());
System.out.println(r1.getPerimeter());
Rectangle r2 = new Rectangle(5 , 10);
  System.out.println(r2.getArea());
  System.out.println(r2.getPerimeter());
 }
}
class Rectangle{
 //1.俩个名为width 和height 的double型数据域,他们分别表示矩形宽和高,width和height默认值都是1
 private  double width = 1;
 private  double height = 1;
 //2.创建默认矩形的无参构造方法
 public Rectangle(){}
 //3.一个创建width 和 height 为指定值得矩形构造方法
 public Rectangle (double width ,double height){
  this.width = width;
  this.height = height;
 }
 //4.一个名为getArea()的方法返回矩形的面积
public  double getArea(){
  return width * height;
}
 //5.一个名为hetperimeter()的方法返回周长
 public double getPerimeter(){
  return 2 *(width + height);
 }
}

三、设计一个名为StopWatch的类,该类包含:
1.具有访问器方法的私有数据域startTime 和endTime
2.一个无参构造方法,使用当前时间来初始化StartTime
3.一个名为start()的方法,将starttime重设为当前时间
4.一个名为stop()的方法,将endtime设置为当前时间
5.一个名为getElapsedTime()的方法,以毫秒为单位返回秒表记录的流逝时间

package Static;

/*
设计一个名为StopWatch的类,该类包含:
1.具有访问器方法的私有数据域startTime 和endTime
2.一个无参构造方法,使用当前时间来初始化StartTime
3.一个名为start()的方法,将starttime重设为当前时间
4.一个名为stop()的方法,将endtime设置为当前时间
5.一个名为getElapsedTime()的方法,以毫秒为单位返回秒表记录的流逝时间
 */
public class Demo107 {
 public static void main(String [] args){
StopWatch sw = new StopWatch();
sw.start();
for (int i = 0; i < 1000000000; i++){
for(int j = 0;j<9999999;j++){

}
}
sw.stop();
System.out.println(sw.getElapsedTime());
 }
}

//设计一个名为StopWatch的类,该类包含:
class  StopWatch{
 //1.具有访问器方法的私有数据域startTime 和endTime
 private long startTime;
 private long endTime;
 //2.一个无参构造方法,使用当前时间来初始化StartTime
 public StopWatch(){
  startTime = System.currentTimeMillis();
 }
 //3.一个名为start()的方法,将starttime重设为当前时间
 public void start(){
  startTime = System.currentTimeMillis();
 }
 //4.一个名为stop()的方法,将endtime设置为当前时间
 public void stop(){
  endTime = System.currentTimeMillis();
 }
 //5.一个名为getElapsedTime()的方法,以毫秒为单位返回秒表记录的流逝时间
 public long getElapsedTime(){
  return endTime - startTime;
 }

 //访问器
 public long getStartTime(){
  return startTime;
 }
 public long getEndTime(){
  return endTime;
 }
}

四、设计一个名为Fan的类来表示一个风扇,这个类包括:
1.三个名为SLOW、MEDIUM和FAST而值为1、2和3的常量,表示风扇的速度
2.一个名为speed的int类型私有数据域,表示风扇的速度(默认值为SLOW)
3.一个名为on的boolean类型私有数据域,表示风扇是否打开(默认值为false)
4.一个名为radius的double类型私有数据域,表示风扇的半径(默认值为5)
5.一个名为color的string类型数据域,表示风扇的颜色(默认值为blue)
这四个数据域的访问器和修改器

package Static;
/*
.设计一个名为Fan的类来表示一个风扇,这个类包括:
1.三个名为SLOW、MEDIUM和FAST而值为1、2和3的常量,表示风扇的速度
2.一个名为speed的int类型私有数据域,表示风扇的速度(默认值为SLOW)
3.一个名为on的boolean类型私有数据域,表示风扇是否打开(默认值为false)
4.一个名为radius的double类型私有数据域,表示风扇的半径(默认值为5)
5.一个名为color的string类型数据域,表示风扇的颜色(默认值为blue)
这四个数据域的访问器和修改器
 */
public class Demo38{
 public static void main(String[] args){
  Fan debug1=new Fan();//为Fan类创建对象debug1
  debug1.setSpeed(3);//调用修改器,修改里面的数据
  debug1.setOn(true);
  debug1.setRadius(10);
  debug1.setColor("yellow");
  System.out.println(debug1.toString());
  Fan debug2=new Fan();//创建第二个对象
  debug2.setSpeed(2);
  debug2.setOn(false);
  debug2.setRadius(5);
  debug2.setColor("blue");
  System.out.println(debug2.toString());
 }
}
class Fan{
 private final int SlOW=1;
 private final int MEDIUM=2;
 private final int FAST=3;
 private int speed;
 private boolean on;
 private double radius;
 private String color;
 Fan(){
  this.speed=SlOW;
  this.on=false;
  this.radius=5;
  this.color="blue";
 }
 public String toString(){
  if(getOn()==false){
   return "Fan is off,color is "+getColor()+",radius is "+getRadius();
  }else{
   return "Fan is on,color is "+getColor()+",radius is "+getRadius()+",speed is "+getSpeed();
  }
 }
 public void setSpeed(int speed){
  this.speed=speed;
 }
 public int getSpeed(){
  return speed;
 }
 public void setOn(boolean on){
  this.on=on;
 }
 public boolean getOn(){
  return on;
 }
 public void setRadius(double radius){
  this.radius=radius;
 }
 public double getRadius(){
  return radius;
 }
 public void setColor(String color){
  this.color=color;
 }
 public String getColor(){
  return color;
 }
}
package Static;

public class Demo108 {
 public static void main(String[] args) {
Fan1 f1 =new Fan1();
  System.out.println(f1.toString());
  f1.setSpeed(Fan1.MEDIUM);
  f1.setOn(true);
  f1.setColor("red");
  f1.setRadius(10);
  System.out.println(f1.toString());
 }
}
class Fan1{
 public static final int SLOW = 1;
 public static final int MEDIUM = 2;
 public static final int FAST = 3;
 private int speed = SLOW;
 private boolean on = false;
 private double radius = 5;
 private String color = "blue";

 public Fan1(){

 }

 public  String toString(){
  if(on){
   return speed +" "+ color +" "+ radius;
  }else {
    return "fan is off" +" "+ color +" "+ radius;
  }
 }

//访问器
 public void  setSpeed(int speed){
  this.speed = speed;
 }
 public void setOn(boolean on){
  this.on = on;
 }
 public void setRadius(double radius){
  this.radius = radius;
 }
 public void setColor(String color){
  this.color = color;
 }
 //修改器
 public int getSpeed(){
  return speed;
 }
 public boolean isOn(){
  return on;
 }

 public double getRadius() {
  return radius;
 }
 public String getColor(){
  return color;
 }
}

五、二次方程式axx+bx+c=0设计一个名为QuadraticEquation的类。这个类包括:
1.代表三个系数的私有数据域a、b和c
2.一个参数为a、b和c的构造方法
3.一个名为getDiscriminant()的方法返回判别式,bb-4ac
4.名为getRoot1()和getRoot2()的方法返回等式的两个根:

package Static;
/*
为二次方程式axx+bx+c=0设计一个名为QuadraticEquation的类。这个类包括:
1.代表三个系数的私有数据域a、b和c
2.一个参数为a、b和c的构造方法
3.一个名为getDiscriminant()的方法返回判别式,bb-4ac
4.名为getRoot1()和getRoot2()的方法返回等式的两个根:
 */
public class Demo39{
 public static void main(String[] args){
  QuadraticEquation q=new QuadraticEquation(2,1,5);
  q.getRoot1();
  q.getRoot2();
 }
}
class QuadraticEquation{
 private double a,b,c;//私有域a、b、c
 public QuadraticEquation(double a,double b,double c){//构造方法
  this.a=a;
  this.b=b;
  this.c=c;
 }
 public double getDiscriminant(){
  return b*b-4*a*c;
 }
 public void getRoot1(){
  double delt=getDiscriminant();
  if(delt<0){
   System.out.println("The equation has no roots.");
  }else if(delt==0){
   System.out.println((-b)/(2*a));
  }else{
   System.out.println((-b+Math.sqrt(delt))/(2*a));
  }
 }
 public void getRoot2(){
  double delt=getDiscriminant();
  if(delt<0){
   System.out.println("The equation has no roots.");
  }else if(delt==0){
   System.out.println((-b)/(2*a));
  }else{
   System.out.println((-b-Math.sqrt(delt))/(2*a));
  }
 }
 //a、b、c的三个get方法
 public double getA(){
  return this.a;
 }
 public double getB(){
  return this.b;
 }
 public double getC(){
  return this.c;
 }
}

六、设计一个名为MyInteger的类。这个类包括
1.一个名为value 的int 型数据域,存储这个对象表示的int值
2.一个指定的int值创建MyInteger对象构造方法
3.一个返回int值得get方法
4.如果值分别为偶数,奇数,素数,那么isEven() isOdd() isprime()方法都会返回ture
5.如果该对象的值与指定的值相等,那么equals (int) 和 equals(MyInteger)方法返回值ture
6.静态方法parseInt(Char[])将数字字符构成的数组转换成一个int值
7.静态方法parseInt(String)将一个字符串转换成一个int值

package Static;

/*
设计一个名为MyInteger的类。这个类包括
1.一个名为value 的int 型数据域,存储这个对象表示的int值
2.一个指定的int值创建MyInteger对象构造方法
3.一个返回int值得get方法
4.如果值分别为偶数,奇数,素数,那么isEven() isOdd() isprime()方法都会返回ture
5.如果该对象的值与指定的值相等,那么equals (int) 和 equals(MyInteger)方法返回值ture
6.静态方法parseInt(Char[])将数字字符构成的数组转换成一个int值
7.静态方法parseInt(String)将一个字符串转换成一个int值
 */
public class Demo113 {
 public static void main(String [] args){
  //自己测自己:
  MyInteger m1 = new MyInteger(3);
  System.out.println(m1.isEven());
  System.out.println(m1.isPrime());
  System.out.println(m1.isOdd());

  //借助工具测:
  MyInteger m2 = new MyInteger(4);
  MyInteger m3 = new MyInteger(13);
  System.out.println(MyInteger.isEven(m2));
  System.out.println(MyInteger.isPrime(m3));

  System.out.println(m2.equals(m3));

  System.out.println(MyInteger.parseInt("1234") +1);
 }
}

class MyInteger {
 //1.一个名为value 的int 型数据域,存储这个对象表示的int值
 private int value;

 // 2.一个指定的int值创建MyInteger对象构造方法
 public MyInteger(int value) {
  this.value = value;
 }

 // 3.一个返回int值得get方法
 public int get() {
  return value;
 }

 // 4.如果值分别为偶数,奇数,素数,那么isEven() isOdd() isprime()方法都会返回ture
 public boolean isEven() {
  return value % 2 == 0;
 }

 public boolean isOdd() {
  return value % 2 == 1;
 }

 public boolean isPrime() {
  for (int i = 2; i < value / 2; i++) {
   if (value % i == 0) {
    return false;
   }
  }
  return true;
 }

 //5.如果该对象的值与指定的值相等,那么equals (int) 和 equals(MyInteger)方法返回值ture
 public static boolean isEven(MyInteger integer) {
  return integer.get() % 2 == 0;
 }

 public static boolean isOdd(MyInteger integer) {
  return integer.get() % 2 == 1;
 }

 public static boolean isPrime(MyInteger integer) {
  for (int i = 2; i < integer.get() / 2; i++) {
   if (integer.get() % i == 0) {
    return false;
   }
  }
  return true;
 }

 public boolean equals(int num) {
  return value == num;
 }

 public boolean equals(MyInteger integer) {
  return value == integer.get();
 }
// 7.静态方法parseInt(String)将一个字符串转换成一个int值
//1234
 public static int parseInt(String str){
  int result = 0;
  for (int i = 0 ; i <str.length() ; i++){
     int num = str.charAt(i) - '0';
     result  = num + result * 10;
  }
  return result;
 }
}
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值