1-IntReceiver类可以接受指定范围内的整数值
IntReceiver类可以接受指定范围内的整数值,具有下面四个属性:
最大的可接受值
最小的可接受值
输入提示字符串
错误消息字符串
并具有一个方法:
getValue(),功能是显示输入提示字符串,然后读取键盘输入的-一个整数。如果读取的
值不在允许范围内,则显示错误消息字符串,并重新要求用户输入一个新值,重复以上步骤
直到输入了一个可接受的值。最后返回读取到的整数。
要求编程实现IntReceiver类,使给定的Test类能正常运行,并实现指定的输出内容。
import java.util.Scanner;
public class IntReceiver {
private int max = 100;
private int min = 0;
private String warningStr = "invalid input!";
private String inputStr = "input an integer:";
public int getValue() {
int temp;
Scanner in = new Scanner(System.in);
System.out.print(inputStr);
temp = in.nextInt();
while (temp < min || temp > max) {
System.out.println(warningStr);
System.out.print(inputStr);
temp = in.nextInt();
}
return temp;
}
}
class Test{
public static void main(String[] args)
{
IntReceiver ir = new IntReceiver();
int value;
value = ir.getValue();
System.out.println("The value is "+value);
}
}
2-汽车类
一辆Car有(has)四个轮子(Wheels)和一个发动机(Engine)。现在要求用组合方法设计类Car、类Wheel和类Engine.
(1) 类Engine 有字符串属性type记录发动机的型号,有构造方法,可设置发动机的型号有方法start()启动引擎(输出下面样例中包含发动机型号和“starts”的字符串)
(2)类Wheel有字符串属性type记录轮胎的型号,有整数类型属性index记录当前轮胎编号(1:front-left,2:front-right,3:back-left,4:back-right),有构造方法,可设置轮胎的型号和编号,有方法roll()表示轮胎正在转动(输出下面样例中包含轮胎型号、轮胎位置和“rolling”的字符串)
(3)类Car有字符串属性model记录轿车的型号,有属性wheels[]和engine,分别是Wheel类对象数组和Engine类对象,有构造方法,参数是三个字符串,分别表示轿车的型号、轮胎型号和发动机的型号,有方法changeWheel()可以改变指定轮胎的型号,有方法start(),先输出下面样例中包含轿车型号和“firing”的字符串,然后调用engine的start(),再调用所有轮胎的roll(),最后显示轿车型号和“running”。
要求编程实现类Car、类Wheel和类Engine,使给定的Test类能正常运行,并实现指定的输出内容。
class Engine{
String type;
Engine(){
}
Engine(String type){
this.type = type;
}
void start() {
System.out.println("Engine" + "<" + type + ">" + " starts!");
}
}
class Wheel{
String type;
int index;
public Wheel(){
}
public Wheel(int a,String b){
index = a;
type = b;
}
public void roll() {
switch (index) {
case 1:
System.out.println("Wheel front-left " + "<" +type +">"+ " is rolling");
break;
case 2:
System.out.println("Wheel front-right " + "<" +type +">"+ " is rolling");
break;
case 3:
System.out.println("Wheel back-left " + "<" +type +">"+ " is rolling");
break;
case 4:
System.out.println("Wheel back-right " + "<" +type +">"+ " is rolling");
break;
default:
System.out.println("error\n");
break;
}
}
}
class Car{
String model;
Wheel[] Wheels = new Wheel[4];//实例化数组
Engine engine=new Engine();//实例化Engine
public Car(String model, String Wheelstype, String engine) {
this.model = model;
for(int i=0;i<4;i++)
{
Wheels[i]=new Wheel(i+1,Wheelstype);
}//实例化数组成员
this.engine.type = engine;
}
void changeWheel(int a, String type) {
Wheels[a - 1].type = type;
}
void start() {
System.out.println("Car" + "<" + model +">"+ " is firing!");
engine.start();
for (int i = 0; i < 4; i++) {
Wheels[i].roll();
}
System.out.println("Car" + "<" + model +">"+ " is running");
}
}
public class Test01 {
public static void main(String[] args){
String wheel="BridgeStone";
String model="BMW";
String engine="Mode L";
Car car1=new Car(model,wheel,engine);
car1.start();
System.out.println("=================");
model="Benz";
engine="Model S";
Car car2=new Car(model,wheel,engine);
car2.changeWheel(2,"Michelin");
car2.start();
}
}
3-Money类
编写Money类,要求具有yuan, jiao, fen三个int类型的属性及相应的置取方法,所表示的金额分别按元角分保存在各个属性中。
另外 ,还具有以下方法:
1 具有重载的四个set()方法,具体要求如下:
(1)参数为int类型,将参数值存入yuan, jiao和fen都置为0;
(2)参数为double类型,将参数值按分做四舍五入,然后分别存入对应的属性;
(3)参数为字符串String,对字符串中的数字做解析后,按分做四舍五入,将金额分别存入对应的属性;
(4)参数为Money类的对象,将参数中的金额分别存入对应的属性。
2 有两个可实现金额计算的方法
(1) times(int n)方法,参数为int,返回值为Money类对象,其中的总金额为当前对象的总金额乘以参数n
(2) add(Money money)方法,参数为Money类对象,返回值为Money类对象,其中的总金额为当前对象的总金额加上参数money中的总金额。
3 有一个静态方法,按照指定格式输出总金额
writeOut(String owner, Money money)方法,输出格式如“owner have/has XX Yuan XX Jiao XX Fen.”的字符串,所输出的的金额是参数money中的总金额。
字符串转浮点数可以使用静态方法:Double.parseDouble(String)。
要求编程实现类Money,使给定的Test类能正常运行,并实现指定的输出内容。
class Money{
private int jiao;
private int fen;
private int yuan;
public Money(){
}
public void set(int yuan){
this.yuan=yuan;
this.jiao=0;
this.fen=0;
}
public void set(double yuan){
double f = Double.parseDouble(String.format("%.2f", yuan));
int b=(int)(f*100);
this.yuan=(int)(b/100);
this.jiao=((int)(b/10))%10;
this.fen=b%10;
}
public void set(String yuan){
double f = Double.parseDouble(yuan);
int b=(int)(f*100);
this.yuan=(int)(b/100);
this.jiao=((int)(b/10))%10;
this.fen=b%10;
}
public void set(Money yuan){
this.yuan=yuan.yuan;
this.jiao=yuan.jiao;
this.fen=yuan.fen;
}
public Money times(int n){
double a;
Money c=new Money();
a=n*(this.yuan+this.jiao*0.1+this.fen*0.01);
int b=(int)(a*100);
c.yuan=(int)(b/100);
c.jiao=((int)(b/10))%10;
c.fen=b%10;
return c;
}
public Money add(Money money){
Money d=new Money();
d.yuan=money.yuan+this.yuan;
d.jiao=money.jiao+this.jiao;
d.fen=money.fen+this.fen;
while (d.fen>10)
{
d.jiao++;
d.fen=d.fen-10;
}
while (d.jiao>10)
{
d.yuan++;
d.jiao=d.jiao-10;
}
return d;
}
public static void writeOut(String owner, Money money){
System.out.println(owner+" have/has "+money.yuan+" Yuan "+money.jiao+" Jiao "+money.fen+" Fen.");
}
}
public class Test02 {
public static void main(String[] args){
Money myMoney = new Money();
Money yourMoney = new Money();
Money hisMoney = new Money();
Money herMoney = new Money();
int amountInt=365;
double amountDouble=254.686;
String amountString="368.244";
myMoney.set(amountInt);
Money.writeOut("I",myMoney);
yourMoney.set(amountDouble);
Money.writeOut("You",yourMoney);
hisMoney.set(amountString);
Money.writeOut("He",hisMoney);
herMoney.set(myMoney);
Money.writeOut("She",herMoney);
herMoney = yourMoney.times(3);
Money.writeOut("She",herMoney);
herMoney = yourMoney.add(hisMoney);
Money.writeOut("She",herMoney);
System.out.println("Remember: A penny saved is a penny earned.");
}
}
4-MyMath类
已知公式如下图,编程求f(x,y,z)的值 ,在Test类中实现x,y,z值 的输入及结果的输出 。
编写MyMath类,实现静态的4个重载方法f(),分别计算不同的函数值,使给定的Test类能正常运行,并实现指定的输出内容。
import java.util.Scanner;
class MyMath{
public static int f(){
return 0;
}
public static int f(int x){
return x*x;
}
public static int f(int x,int y){
return x*x + y*y;
}
public static int f(int x,int y,int z){
return x*x + y*y +z*z;
}
}
public class Test03 {
public static void main(String[] args){
int x, y, z;
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
in.close();
int result;
if (x < 0) {
result = MyMath.f();
} else if (x >= 0 && y < 0) {
result = MyMath.f(x);
} else if (x >= 0 && y >= 0 && z < 0) {
result = MyMath.f(x, y);
} else {
result = MyMath.f(x, y, z);
}
System.out.println(result);
}
}