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;
 }
}
java集包括有答案 引 言 6 第一章 JAVA基础 7 一、填空 7 二、判断 9 三、选择 9 四、程序阅读 26 第二章 控制结构 32 一、选择 32 二、填空 48 三、判断 50 四、程序阅读 51 五、编程 55 第三章 字符串 59 一、选择 59 二、填空 63 三、判断 64 四、编程 65 第四章 数组 66 一、选择 66 二、判断 69 三、填空 70 四、编程 71 第五章 类和对象 73 一、选择 73 二、填空 79 三、程序填空 80 四、问答 93 五、判断 94 六、编程 94 第六章 面向对象程序设计 97 一、选择 97 二、填空 118 三、判断 120 四、编程 123 第七章 数据库的连接 129 一、选择 129 二、填空 131 三、判断 131 四、编程 131 第八章 多线程 133 一、填空 133 二、选择 134 三、多项选择 147 四、判断 147 五、程序分析 148 六、程序设计 149 第九章 异常处理 151 一、选择 151 二、填空 155 三、判断 157 四、程序阅读 158 五、程序 163 第十章 文件操作 165 一、选择 165 二、填空 170 三、判断 174 四、读程序 177 五、问答 182 六、编程 183 第十一章GUI界面设计 185 一、选择 185 二、填空 194 三、判断 196 四、程序填空 197 五、简答 214 六、编程 214 参考答案 230 第一章 java基础 230 一、填空 230 二、判断 232 三、选择 232 四、程序阅读 232 第二章 控制结构 233 一、选择 233 二、填空 233 三、判断 235 四、程序阅读 235 五、编程 236 第三章 字符串 255 一、选择 255 二、填空 255 三、判断 255 四、编程 255 第四章 数组 265 一、选择 265 二、判断 265 三、填空 266 四、编程 266 第五章 类和对象 269 一、选择 269 二、填空 270 三、程序填空 271 五、判断 274 六、编程 274 第六章 面向对象程序设计 293 一、选择 293 二、填空 294 三、判断 295 四、编程 296 第七章 数据库的连接 352 一、选择 352 二、填空 352 三、判断 353 四、编程 353 第八章 多线程 356 一、填空 356 二、选择 357 三、多项选择 357 四、判断 357 五、程序分析 357 六、程序设计 357 第九章 异常处理 391 一、选择 391 二、填空 391 三、判断 392 四、程序阅读 392 五、程序 393 第十章 文件操作 396 一、选择 396 二、填空 397 三、判断 398 四、读程序 398 五、问答 399 六、编程 401 第十一章 GUI界面设计 417 一、选择 417 二、填空 417 三、判断 419 四、程序填空 419 五、简答 420 六、编程 422
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值