JAVA语言程序设计(基础篇) 第十版——第九章 对象和类 (参考答案)

提示:第9~13章的练习题要达到下面三个目标:

1.设计类并画出UML类图;

2.实现UML中的类;

3.实用类开发应用程序.

个人建议:去下载个软件画UML类图,如,

里面可直接将你画的UML类图转换为代码,省时便捷,具体转换操作去百度百度。

(9.2~9.5节)

9.1(矩形类Rectangle)

JAVA语言程序设计(基础篇) 第十版——第九章 对象和类 (参考答案)_第1张图片

package p9;

public class Rectangle {
	
	public double width;
	public double height;
	
	public Rectangle() {
		width=1;
		height=1;
	}
	
	public Rectangle(double newWidth, double newHeight) {
		width=newWidth;
		height=newHeight;
	}
	
	public double getArea() {
		return width*height;
	}
	
	public double getPerimeter() {
		return 2*(width+height);
	}

}
package p9;

public class Test1 {

	public static void main(String[] args) {
		//创建对象 1
		Rectangle object1=new Rectangle(4, 40);
		
		//创建对象 2
		Rectangle object2=new Rectangle(3.5, 35.9);
		
		System.out.print("第一个矩形: ");
		System.out.print(" 宽:"+object1.width+"  高:"+object1.height);
		System.out.println("  面积:"+object1.getArea()+" 周长:"+object1.getPerimeter());  

		
		System.out.print("第二个矩形: ");
		System.out.print(" 宽:"+object2.width+"  高:"+object2.height);
		System.out.println("  面积:"+object2.getArea()+" 周长:"+object2.getPerimeter());  
		
	}

}

 

9.2(股票类 Stock)

JAVA语言程序设计(基础篇) 第十版——第九章 对象和类 (参考答案)_第2张图片

package p9;

public class Stock {

	public String symbol;
	public String name;
	
	public double previousClosingPrice;
	public double currentPrice;
	
	public Stock(String newSymbol, String newName) {
		symbol=newSymbol;
		name=newName;
	}
	
	public double getChangePercent() {
		return (currentPrice-previousClosingPrice)/100 ;
		
	}
	
	public void setPreviousClosingPrice(double newPreviousClosingPrice) {   
		previousClosingPrice=newPreviousClosingPrice;
	}
	
	public void setCurrentPrice(double CurrentPrice) {
		currentPrice=CurrentPrice;
	}
}
package p9;

public class Test2 {

	public static void main(String[] args) {
		//创建一个对象
		Stock object=new Stock("ORCL","Oracle Corporation");
		object.setPreviousClosingPrice(34.5);
		object.setCurrentPrice(35.5);
		
		System.out.println("市值变化的百分比是:"+object.getChangePercent());
	}

	

}

 

(9.6节)

*9.3(使用日期类Date)

package p9;

public class Test3 {

	public static void main(String[] args) {
		//创建一个Date类
		java.util.Date date=new java.util.Date();
		
		
			date.setTime(10000);
			System.out.println(date.toString());
			
			date.setTime(100000);
			System.out.println(date.toString());
			
			date.setTime(1000000);
			System.out.println(date.toString());
			
			date.setTime(10000000);
			System.out.println(date.toString());
			
			date.setTime(100000000);
			System.out.println(date.toString());
			
			date.setTime(1000000000);
			System.out.println(date.toString());
			
			//注意:长整数要在后面加上“L”,不然不能被识别
			date.setTime(10000000000L);
			System.out.println(date.toString());
			
			//注意:长整数要在后面加上“L”,不然不能被识别
			date.setTime(100000000000L);
			System.out.println(date.toString());
			
			
		
	}

}

 

*9.4(使用随机类Random)

package p9;

import java.util.Random;

public class Test4 {

	public static void main(String[] args) {
		
		Random random=new Random(1000);
		
		for(int i=1; i<=50; i++) {
			if(i%10==0)
				System.out.println(random.nextInt(100));
			else 
				System.out.print(random.nextInt(100)+" ");
		}
	}

}

*9.5(使用公历类GregorianCalendar)

在构造方法里我直接输入了 我的当前日期

因为我发现,用它的无参构造方法时,显示的月份少一个月,我也不知道为什么。

package p9;

import java.util.GregorianCalendar;

public class Test5 {

	public static void main(String[] args) {
		//在构造方法里我直接输入了 我的当前日期
		GregorianCalendar object=new GregorianCalendar(2020,4,8);
		
		System.out.println(object.get(GregorianCalendar.YEAR)+"年"
				+ object.get(GregorianCalendar.MONTH)+"月"
				+ object.get(GregorianCalendar.DATE)+"日");

		
		object.setTimeInMillis(1234567898765L);
		
		System.out.println(object.get(GregorianCalendar.YEAR)+"年"
				+ object.get(GregorianCalendar.MONTH)+"月"
				+ object.get(GregorianCalendar.DATE)+"日");
		
	}

}

 

(9.7~9.9节)

*9.6(秒表)

JAVA语言程序设计(基础篇) 第十版——第九章 对象和类 (参考答案)_第3张图片

JAVA语言程序设计(基础篇) 第十版——第九章 对象和类 (参考答案)_第4张图片

package p9;

public class StopWatch {

		private long startTime;
		private long endTime;
		
		public StopWatch() {
			java.util.Date date=new java.util.Date();
			startTime=date.getTime();
		}
		
		public void start(long startTime) {
			 this.startTime=startTime;
		}
		
		public void Stop(long endTime) {
			this.endTime=endTime;
		}
		
		
		public long getElapsedTime() {
			java.util.Date time=new java.util.Date(); 
			return time.getTime();
			
		}
		
		public long getStartTime() {
			return startTime;
			
		}
		
		public long getEndTime() {
			return endTime;
			
		}
		
}
package p9;

public class Test6 {

	public static void main(String[] args) {
		
		StopWatch object=new StopWatch();
	
		
		int[] n=new int[100000];
		for(int i=0; i<100000; i++) {
			n[i]=(int) (Math.random()*100);
		}
		
		selectionSort(n);
		
		object.Stop(object.getElapsedTime());

		long executeTime=object.getEndTime()-object.getStartTime();  
		System.out.println("执行时间为: "+executeTime+" Milliseconds.");
	}
	
	public static void selectionSort(int[] list) {
		
		for(int i=0;i

 

9.7(账户类Account)

JAVA语言程序设计(基础篇) 第十版——第九章 对象和类 (参考答案)_第5张图片

 

JAVA语言程序设计(基础篇) 第十版——第九章 对象和类 (参考答案)_第6张图片

 

package p9;

import java.util.Date;

public class Account {
		
	public int id=0;//用户名
	public double balance=0;//余额
	public double annualInteresRate=0;//当前利率
	public java.util.Date dateCreated;//存储开户日期
	
	//无参构造方法
	public Account(){
		Date dateCreated=new Date();
		this.dateCreated=dateCreated;
	}
	
	//有参构造方法
	public Account(int id, double balance){
		Date dateCreated=new Date();
		this.dateCreated=dateCreated;
		this.id=id;
		this.balance=balance;
	}
	
	public int getId(){
		return id;
		
	}
	
	public double getBalance() {
		return balance;
	}
	
	
	public double getAnualInterestRate() {
		return annualInteresRate;
		
	}
	
	
	public void setId(int id){
		this.id=id;
	}
	
	
	public void setBalance(double balance) {
		this.balance=balance;
	}
	
	//设置 年利率
	public void setAnnualInterestRate(double annualInterestRate) {
		this.annualInteresRate=annualInterestRate;
	}

	//获得开户日期
	public String getDateCreated(){
		return dateCreated.toString();
		}
	
	//月利息
	public double getMonthlyInterestRate() {
		return annualInteresRate/12;
		
	}
	
	//取钱
	public double withDraw(double withdraw) {
		return this.balance=this.balance-withdraw;
	}
	
	//存钱
	public double deposit(double deposit) {
		return this.balance=this.balance+deposit;
	}

	
}
package p9;

import java.util.Date;

public class Test7 {

	public static void main(String[] args) {
		
		Account object=new Account( 1122, 20000 );
	
		object.setAnnualInterestRate(4.5/100);
		
		object.withDraw(2500);
		
		object.deposit(3000);
		
		System.out.println("余额:"+object.getBalance()+"美元");
		System.out.println("月利息:"+object.getMonthlyInterestRate()*100+"%");   
		System.out.print("开户时间:"+object.getDateCreated());
		
		
	}

}

 

9.8(风扇类 Fan)

JAVA语言程序设计(基础篇) 第十版——第九章 对象和类 (参考答案)_第7张图片

package p9;

public class Fan {
	//常量,表示风扇的速度
	public final  int SLOW=1;
	public final  int MEDIUM=2;
	public final  int FAST=3;
	//数据域
	private int speed=SLOW;
	private boolean on=false;
	private double radius=5;
	public String color="blue";
	
	public int getSpeed(int speed) {
		return speed;
		
	}
	
	public boolean getOn() {
		return false;
		
	}
	
	public double getRadius() {
		return radius;
		
	}
	
	public String getColor() {
		return color;
		
	}
	
	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;
	}
	
	Fan(){
		
	}
	
	public String toString(){
		if(on==true) {
			return "速度:"+this.speed+" 颜色:"+this.color+" 半径:"+this.radius;  
		}
		else {
			return "fan is off."+"  颜色:"+this.color+" 半径:"+this.radius;
		}
	}
	
}
package p9;

public class Test8 {
	private static final int SLOW = 1;
	private static final int MEDIUM = 2;
	private static final int FAST = 3;

	public static void main(String[] args) {
		Fan object1=new Fan();
		Fan object2=new Fan();
		
		object1.setSpeed(FAST);
		object1.setRadius(10);
		object1.setColor("yellow");
		object1.setOn(true);
		
		object2.setSpeed(MEDIUM);
		object2.setRadius(5);
		object2.setColor("blue");
		object2.setOn(false);
		
		System.out.println("对象1: "+object1.toString());
		
		System.out.println("对象2:"+object2.toString());

	}

}

 

**9.9(几何:正 n 边形)

JAVA语言程序设计(基础篇) 第十版——第九章 对象和类 (参考答案)_第8张图片

package p9;

/** */
public class RegularPolygon
{
    //多边形边数
    private int n=3;
    
    //多边形长度
    private double side=1;
    
    //多边形的X坐标
    private double x=0;
    
    //多边形的y坐标
    private double y=0;
    
    //无参构造方法
    RegularPolygon()
    {
    
    }
    
    //指定边数、长度、中心在(0,0)的正多边形的构造方法
    RegularPolygon(int n, double side)
    {
    this.n=n;
    this.side=side;
    }
    
   //指定边数、长度、中心在(x,y)的正多边形的构造方法
    RegularPolygon(int n, double side, double x, double y)
    {
    	this.n=n;
        this.side=side;
        this.x=x;
        this.y=y;
    }
    
    /** */
    public int getN()
    {
		return n;
    
    }
    
    /** */
    public void setN(int n)
    {
    this.n=n;
    }
    
    /** */
    public double getSide()
    {
		return side;
    
    }
    
    /** */
    public void setSide(double side)
    {
    this.side=side;
    }
    
    /** */
    public double getX()
    {
		return x;
    
    }
    
    /** */
    public void setX(double x)
    {
    this.x=x;
    }
    
    /** */
    public double getY()
    {
		return y;
    
    }
    
    /** */
    public void setY(double y)
    {
    this.y=y;
    }
    
    //求周长
    public double getPerimeter()
    {
		return n*side;
		
    }
    
    //求面积
    public double getArea()
    {
		return (n*Math.pow(side, 2)) / ( 4 * Math.toDegrees( Math.tan(Math.PI/n ) ) );   
    
    }
}

package p9;

public class Test9 {

	public static void main(String[] args) {
		
		RegularPolygon object1=new RegularPolygon();
		RegularPolygon object2=new RegularPolygon(6,4);
		RegularPolygon object3=new RegularPolygon(10,4,5.6,7.8);
	
		System.out.println("对象1: 周长:"+object1.getPerimeter()+"  面积:"+object1.getArea());   
	
		System.out.println("对象2: 周长:"+object2.getPerimeter()+"  面积:"+object2.getArea());   
	
		System.out.println("对象3: 周长:"+object3.getPerimeter()+"  面积:"+object3.getArea());   
	
	
	}

}

 

*9.10(代数:二次方程)

JAVA语言程序设计(基础篇) 第十版——第九章 对象和类 (参考答案)_第9张图片

package p9;

public class QuadraticEquation {
	private double a;
	private double b;
	private double c;
	
	QuadraticEquation(double a, double b, double c) {
	this.a=a;
	this.b=b;
	this.c=c;
	}
	
	public double getA() {
		return a;
	
	}
	
	public double getB() {
		return b;
	
	}
	
	public double getC() {
		return c;
	
	}
	
	public double getDiscriminant() {
	double p=Math.pow(b, 2)-4*a*c;
	return p;
	}
	
	public double getRoot1() {
	double r1=(-b+Math.sqrt(Math.pow(b, 2)-4*a*c)) / (2*a); 
	return r1;
	}
	
	public double getRoot2() {
	double r2=(-b-Math.sqrt(Math.pow(b, 2)-4*a*c)) / (2*a); 
	return r2;
	}
}
package p9;

import java.util.Scanner;

public class Test10 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("请输入a、b和c的值:");
		double a=input.nextDouble();
		double b=input.nextDouble();
		double c=input.nextDouble();
		input.close();
		
		QuadraticEquation object=new QuadraticEquation(a,b,c);
		
		
		System.out.println("a的值:"+object.getA());
		System.out.println("b的值:"+object.getB());
		System.out.println("c的值:"+object.getC());
		
		if(object.getDiscriminant()>0)
			System.out.println("判别式>0,方程有两个根,分别为  r1:"+object.getRoot1()+" r2:"+object.getRoot2());
		else if(object.getDiscriminant()==0)
			System.out.println("判别式=0,方程只有一个根 r:"+object.getRoot1());
		else
			System.out.println("The equation has no roots.");
	}

}

 

*9.11(代数:2x2 的线性方程)

JAVA语言程序设计(基础篇) 第十版——第九章 对象和类 (参考答案)_第10张图片

package p9;

public class LinearEquation {
	private double a;
	private double b;
	private double c;
	private double d;
	private double e;
	private double f;
	
	LinearEquation(double a, double b, double c, double d, double e, double f) {
	this.a=a;
	this.b=b;
	this.c=c;
	this.d=d;
	this.e=e;
	this.f=f;
	}
	
	public double getA() {
		return a;
	
	}
	
	public double getB() {
		return b;
	
	}
	
	public double getC() {
		return c;
	
	}
	
	public double getD() {
		return d;
	
	}
	
	public double getE() {
		return e;
	
	}
	
	public double getF() {
		return f;
	
	}
	
	public boolean isSolvable() {
	if((this.a*this.d-this.b*this.c) != 0)
		return true;
	else
		return false;
	}
	
	public double getX() {
		double x=(this.e*this.d-this.b*this.f)/(this.a*this.d-this.b*this.c);
		return x;
	}
	
	public double getY() {
		double y=(this.a*this.f-this.e*this.c)/(this.a*this.d-this.b*this.c);
		return y;
	}
}
package p9;

import java.util.Scanner;

public class Test11 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("请输入a、b、c、d、e、f的值:");
		double a=input.nextDouble();
		double b=input.nextDouble();
		double c=input.nextDouble();
		double d=input.nextDouble();
		double e=input.nextDouble();
		double f=input.nextDouble();
		
		LinearEquation object=new LinearEquation(a,b,c,d,e,f);
		
		if(object.isSolvable()) 
			System.out.println("X:"+object.getX()+" y:"+object.getY());
		else
			System.out.println("The equation has no solution.");
	
	}

}

 

**9.12(几何:交点)

在上一题的基础上,我增加了两个数据域 g 和 h,两个方法:getG( )方法 和 getH( )方法,用来获得4个点的坐标

两点确定一条直线,点1(x1,y1)和点2(x2,y2)确定直线L1;

                               点3(x3,y3)和点4(x4,y4)确定直线L2;

利用(3.3题)的公式:

                                 ax+by=e      cx+dy=f

  解得 

                                 x=(ed-bf)/(ad-bc)           y=(af-ec)/(ad-bc)            

其交点就是:(x,y)

若(ad-bc)=0,则说明这两条直线平行。

理解了以上公式,再带入(3.25题)的公式,即可求解。

在(3.25题)中,公式可这样理解:

a=(y1-y2)

b= -(x1-x2)

c=(y3-y4)

d= -(x3-x4)

e=(y1-y2)x1-(x1-x2)y1

f=(y3-y4)x3-(x3-x4)y3

JAVA语言程序设计(基础篇) 第十版——第九章 对象和类 (参考答案)_第11张图片

package p9;

public class LinearEquation {
	
	private double x1;
	private double y1;
	private double x2;
	private double y2;
	private double x3;
	private double y3;
	private double x4;
	private double y4;
	
	LinearEquation(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
	this.x1=x1;
	this.y1=y1;
	this.x2=x2;
	this.y2=y2;
	this.x3=x3;
	this.y3=y3;
	this.x4=x4;
	this.y4=y4;
	}
	
	public double getX1() {
		return x1;
	}
	
	public double getY1() {
		return y1;
	}
	
	public double getX2() {
		return x2;
	}
	
	public double getY2() {
		return y2;
	}
	
	public double getX3() {
		return x3;
	}
	
	public double getY3() {
		return y3;
	}
	
	public double getX4() {
		return x4;
	}
	
	public double getY4() {
		return y4;
	}
	
	public boolean isSolvable() {
	if(((y1-y2)*-(x3-x4))-(-(x1-x2)*(y3-y4)) != 0)
		return true;
	else
		return false;
	}
	
	public double getX() {
		double x=(((y1-y2)*x1-(x1-x2)*y1)*( -(x3-x4))-( -(x1-x2))*((y3-y4)*x3-(x3-x4)*y3))
				/  (((y1-y2)*-(x3-x4))-(-(x1-x2)*(y3-y4))) ; 
		return x;
	}
	
	public double getY() {
		double y=(((y1-y2))*((y3-y4)*x3-(x3-x4)*y3)-((y1-y2)*x1-(x1-x2)*y1)*((y3-y4)))
				/  (((y1-y2)*-(x3-x4))-(-(x1-x2)*(y3-y4))) ;
		return y;
	}
}
package p9;

import java.util.Scanner;

public class Test12 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("请输入坐标:x1,y1,x2,y2,x3,y3,x4,y4的值:");
		
		double x1=input.nextDouble();
		double y1=input.nextDouble();
		double x2=input.nextDouble();
		double y2=input.nextDouble();
		double x3=input.nextDouble();
		double y3=input.nextDouble();
		double x4=input.nextDouble();
		double y4=input.nextDouble();
		input.close();
		
		//创建对象
		LinearEquation object=new LinearEquation(x1,y1,x2,y2,x3,y3,x4,y4);
		
		if(object.isSolvable())
			System.out.print("The intersecting point is at (" + object.getX() + ", " + object.getY() + ")");
		else
			System.out.print(" The two lines are parallel ");

	}

}

 

**9.13(位置类 Location)

JAVA语言程序设计(基础篇) 第十版——第九章 对象和类 (参考答案)_第12张图片

JAVA语言程序设计(基础篇) 第十版——第九章 对象和类 (参考答案)_第13张图片

 

package p9;

public class Location {
	public int row;
	public int column;
	public double maxValue;
	
	public static String locateLargest(double[][] a) {
	
		
		double maxValue=a[0][0];
		int row=0;
		int column=0;
		
		for(int i=0; imaxValue) {
					maxValue=a[i][j];
					row=i;
					column=j;
				}
					
				
			}
		}
		
		return "The location of the largest element is "
					+maxValue+" at ("+row+", "+column+")";       
	
	}

	
}
package p9;

import java.util.Scanner;

public class Test13 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the number of rows and columns in the array:");  
		int rows=input.nextInt();
		int columns=input.nextInt();
		
		double[][] n=new double[rows][columns];
		
		System.out.println("Enter the array");
		
		for(int row=0; row

 

你可能感兴趣的:(JAVA编程,java,程序设计,经验分享,程序人生)