类与对象
来源:互联网 发布:CCD软件接口GiGe 编辑:程序博客网 时间:2024/06/05 06:54
1.【矩形类Rectangle】遵照9.2节中Circle类的例子,设计一个名为Rectangle的类表示矩形。这个类包括:
两个名为width和height的double型数据域,它们分别表示矩形的宽和高。width和height的默认值都为1。
创建默认矩形的无参构造方法。
创建width和height为指定值的矩形的构造方法。
一个名为getArea()的方法返回这个矩形的面积。
一个名为getPerimeter()的方法返回矩形周长。
画出该类的UML图并实现这个类。编写一个测试程序,创建两个Rectangle对象:一个矩形的宽为4高为40,另一个矩形的宽为3.5高为35.9。依次显示每个矩形的宽、高、面积和周长。
代码:
public class I {
public static void main(String[] args) { // TODO Auto-generated method stub //创建第一个Rectangle对象 Rectangle r1 = new Rectangle(4,40); System.out.println("r1的宽为:" + r1.width); System.out.println("\nr1的高为:" + r1.height); System.out.println("\nr1的面积为:" + r1.getArea()); System.out.println("\nr1的周长为:" + r1.getPerimeter()); //创建第二个Rectangle对象 Rectangle r2 = new Rectangle(3.5,35.9); System.out.println("2的宽为:" + r2.width); System.out.println("\nr2的高为:" + r2.height); System.out.println("\nr2的面积为:" + r2.getArea()); System.out.println("\nr2的周长为:" + r2.getPerimeter());}
}
class Rectangle{
double width;
double height;
//无参构造方法Rectangle(){ width = 1; height = 1;}//有参构造方法Rectangle(double newWidth,double newHeight){ width = newWidth; height = newHeight;}//返回矩形的面积double getArea(){ return width * height;}//返回矩形的周长double getPerimeter(){ return 2 *(width + height);}
}
2. 【风扇类Fan】设计一个名为Fan的类表示一个风扇。这个类包括:
三个名为SLOW、MEDIUM和FAST而值为1、2、3的常量表示风扇的速度。
一个名为speed的int类型私有数据域表示风扇的速度(默认值为SLOW)。
一个名为on的boolean类型私有数据域表示风扇是否打开(默认值为false)。
一个名为radius的double类型私有数据域表示风扇的半径(默认值为5)。
一个名为color的String类型数据域表示风扇的颜色(默认值为blue)。
这四个数据域的访问器和修改器。
一个创建默认风扇的无参构造方法。
一个名为toString()的方法返回描述风扇的字符串。如果风扇是打开的,那么该方法在一个组合的字符串中返回风扇的速度、颜色和半径。如果风扇没有打开,该方法返回一个由“fan is off”和风扇颜色、半径组成的字符串。
画出该类的UML图。实现这个类。编写一个测试程序,创建两个Fan对象。将第一个对象设置为最大速度、半径为10、颜色为yellow、状态为打开。将第二个对象设置为中等速度、半径为5、颜色为blue、状态为关闭。通过调用它们的toString方法显示这些对象。
代码:
public class II {
public static void main(String[] args) { // TODO Auto-generated method stub
Fan f1 =new Fan(3,true,10,”yellow”);
Fan f2 =new Fan(2,false,5,”blue”);
System.out.println(f1.toString());
System.out.println(f2.toString());
}
}
class Fan{
private int speed ;
private boolean on ;
private double radius;
private String color;
final int SLOW=1;
final int MEDIUM=2;
final int FAST=3;
//Construct a fan with default values Fan(){ speed = 1; on = false; radius = 5; color = "blue"; } //Construct a fan with specified values Fan(int newSpeed,boolean newOn,double newRadius,String newColor){ speed = newSpeed; on = newOn; radius = newRadius; color = newColor; } //get speedpublic int getSpeed(){ return speed; }//get on public boolean getOn(){ return on; } //get radiuspublic double getRadius(){ return radius; } //get colorpublic String getColor(){ return color; } //set speedpublic void setSpeed(int newSpeed){ speed=newSpeed; }//set onpublic void setOn(boolean newOn){ on=newOn;}//set radiuspublic void setRadius(double newRadius){ radius=newRadius;}//set colorpublic void setColor(String newColor){ color = newColor;} //Return description of the fanpublic String toString(){ if(on) return("The speed of the fan is " + speed + " and the color of the fan is " + color + " and the radius of the fan is " + radius); else return("Fan is off"); }
}
3.【二次方程式】为二次方程式ax2+bx+c=0设计一个名为QuadraticEquation的类。这个类包括:
代表三个系数的私有数据域a、b、c。
一个参数为a、b、c的构造方法。
a、b、c的三个get方法。
一个名为getDiscriminant()的方法返回判别式,b2-4ac。
一个名为getRoot1()和getRoot2()的方法返回等式的两个根。
这些方法只有在判别式为非负数时才有用。如果判别式为负,方法返回0。
画出该类的UML图。实现这个类。编写一个测试程序,提示用户输入a、b、c的值,然后显示判别式的结果。如果判别式为正数,显示两个根;如果判别式为0,显示一个根;否则,显示“The equation has no roots”。
代码:import java.util.Scanner;
public class III {
public static void main(String[] args) { // TODO Auto-generated method stub
System.out.println(“Enter a,b,c:”);
Scanner input=new Scanner(System.in);
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
QuadraticEquation Q = new QuadraticEquation(a,b,c);
System.out.println(“判别式的值为” + Q.getDiscriminant());
if(Q.getDiscriminant()>0){
System.out.println(“root1 is ” + Q.getRoot1() + ” root2 is ” + Q.getRoot2());
}
if(Q.getDiscriminant()==0)
System.out.println(“root is ” + Q.getRoot1());
if(Q.getDiscriminant()<0)
System.out.println(“The equation has no roots “);
}
}
class QuadraticEquation{
private double a;
private double b;
private double c;
//Construct a QuadraticEquation QuadraticEquation(double a1,double b1,double c1){ a = a1; b = b1; c = c1;}//Get adouble getA(){ return a;}//Get bdouble getB(){ return b;}//Get cdouble getC(){ return c;}//return discriminantdouble getDiscriminant(){ return (b*b-4*a*c);}//Get root1double getRoot1(){ if( getDiscriminant()>=0){ return ((-b+Math.sqrt(getDiscriminant()))/(2*a)); } else return 0;}//Get root2double getRoot2(){ if( getDiscriminant()>=0){ return ((-b-Math.sqrt(getDiscriminant()))/(2*a)); } else return 0;}
}
4.【位置类】设计一个名为Location的类,定位二维数组中的最大值及其位置。这个类包括公共的数据域row、column和maxValue,二维数组中的最大值及其下标用double型的maxValue以及int型的row和column存储。
编写下面的方法,返回一个二维数组中最大值的位置。
public static Location locateLargetst(double[][] a)
返回值是一个Location的实例。编写一个测试程序,提示用户输入一个二维数组,然后显示这个数组中的最大元素及下标。运行实例如下:
输入二维数组的行数和列数: 3 4
输入数组:
23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
最大元素及其下标是: 45 在(1,2)
代码:import java.util.Scanner;
public class IV {
public static void main(String[] args) { // TODO Auto-generated method stub System.out.print("Enter the number of row and columns in the array: "); Scanner input=new Scanner(System.in); int r = input.nextInt(); int c = input.nextInt(); System.out.println("Enter the array:"); double [][]a=new double[r][c]; for(int i=0;i<a.length;i++){ for(int k=0;k<a[i].length;k++){ a[i][k]=input.nextDouble(); } }Location ic=new Location();ic=ic.locateLargetst(a);System.out.print("The location of the largest element is "+ic.maxValue+"at "+"("+ic.row+","+ic.column+")");}
}
class Location{
public int row;
public int column;
public double maxValue;
public static Location locateLargetst(double[][] a){ int r=0; int c=0; double maxValue1=a[r][c]; for(int i=0;i<a.length;i++){ for(int k=0;k<a[i].length;k++){ if(a[i][k]>maxValue1){ r=i; c=k; maxValue1=a[r][c]; } } } Location l=new Location(); l.row=r; l.column=c; l.maxValue= maxValue1; return l;}
}
- OOP-----类与对象对象
- OOP-----类与对象对象
- 对象与元类对象
- 对象与类类型
- C02(类与对象)
- 类与对象(PHP5)
- 类与对象
- 区分类与对象
- 类与对象
- 类与对象
- 类与对象
- C#类与对象
- 对象与类
- 类与对象总结
- js类与对象
- C++ 类与对象
- 类与对象
- 类与对象概述
- 写jquery插件
- POJ 2404 Jogging Trails 笔记
- Python2与Python3的编码问题
- XML中shape的使用
- 一个屌丝程序猿的人生(十二)
- 类与对象
- opencv库杂集
- 记录caffe在ubuntu16.04系统上面的安装全程
- python:委托->包装对象
- 揭秘 Cortex-A55,为何它是对未来数字世界举足轻重的处理器?
- 一个屌丝程序猿的人生(十三)
- Redis 与哈希(Hash)相关的命令
- 人工智能如何学
- socketpair和dup---高级I/O