一、static关键字:
package Static;
public class Dome {
public static void main(String [] args){
A a = new A();
System.out.println(a ==a .a);
System.out.println(a.a == a.a.a);
}
}
class A{
static A a = new A();
}
二. 1.俩个名为width 和height 的double型数据域,他们分别表示矩形宽和高,width和height默认值都是1 2.创建默认矩形的无参构造方法 3.一个创建width 和 height 为指定值得矩形构造方法 4.一个名为getArea()的方法返回矩形的面积 5.一个名为hetperimeter()的方法返回周长
package Static;
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{
private double width = 1;
private double height = 1;
public Rectangle(){}
public Rectangle (double width ,double height){
this.width = width;
this.height = height;
}
public double getArea(){
return width * height;
}
public double getPerimeter(){
return 2 *(width + height);
}
}
三、设计一个名为StopWatch的类,该类包含: 1.具有访问器方法的私有数据域startTime 和endTime 2.一个无参构造方法,使用当前时间来初始化StartTime 3.一个名为start()的方法,将starttime重设为当前时间 4.一个名为stop()的方法,将endtime设置为当前时间 5.一个名为getElapsedTime()的方法,以毫秒为单位返回秒表记录的流逝时间
package Static;
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());
}
}
class StopWatch{
private long startTime;
private long endTime;
public StopWatch(){
startTime = System.currentTimeMillis();
}
public void start(){
startTime = System.currentTimeMillis();
}
public void stop(){
endTime = System.currentTimeMillis();
}
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;
public class Demo38{
public static void main(String[] args){
Fan debug1=new Fan();
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;
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;
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));
}
}
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;
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 {
private int value;
public MyInteger(int value) {
this.value = value;
}
public int get() {
return value;
}
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;
}
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();
}
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;
}
}
|