Java语言程序设计与数据结构(基础篇)——梁勇 第九章编程练习题答案(自写)

9.1 Rectangle类

package ChapterNine;

//	Rectangle类
public class Rectangle {
	//数据域
	double width = 1;
	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;
	}
}

测试类

package ChapterNine;

public class RectangleTest {
	public static void main(String[] args) {
		Rectangle rectangle1 = new Rectangle(4, 40);
		Rectangle rectangle2 = new Rectangle(3.5, 35.9);

		System.out.println(rectangle1.width + " " + rectangle1.height + " " + rectangle1.getArea() + " "
				+ rectangle1.getPerimeter());
		System.out.println(rectangle2.width + " " + rectangle2.height + " " + rectangle2.getArea() + " "
				+ rectangle2.getPerimeter());

	}
}

9.2 Stock类

package ChapterNine;

//股票类
public class Stock {
	//	数据域
	String symbol;
	String name;
	double previousClosingPrice;
	double currentPrice;

	//	构造方法
	public Stock(String symbol, String name) {
		this.symbol = symbol;
		this.name = name;
	}

	// 输入previousClosingPrice,currentPrice
	public void setPPriceAndCPrice(double previousClosingPrice, double currentPrice) {
		this.previousClosingPrice = previousClosingPrice;
		this.currentPrice = currentPrice;
	}

	// 获取变化百分比
	public double getChangePercent() {
		return 1 - (currentPrice / previousClosingPrice);
	}

}

测试类

package ChapterNine;

public class StockTest {
	public static void main(String[] args) {
		Stock orachleCorporation = new Stock("ORCL", "Oracle Corporation");
		orachleCorporation.setPPriceAndCPrice(34.5, 34.35);
		System.out.println(orachleCorporation.getChangePercent() * 100 + "%");
	}
}

9.3 Date类

package ChapterNine;

public class Date {
	//时间对象
	public static void main(String[] args) {
		java.util.Date date1 = new java.util.Date(10000);
		System.out.println(date1.toString());
	}
}

9.4 Random类

package ChapterNine;
//import java.util.Random;

public class Random {
	public static void main(String[] args) {
		java.util.Random random1=new java.util.Random();
		for(int i=1;i<=50;i++) {
			System.out.println(random1.nextInt(100));
		}
	}

}

9.5 GregorianCalendar类

package ChapterNine;

import java.util.*;

public class GregorianCalendar {
	public static void main(String[] args) {
		java.util.GregorianCalendar date1 = new java.util.GregorianCalendar();
		int month = date1.get(Calendar.MONTH) + 1;
		System.out.println(date1.get(Calendar.YEAR) + "年" + month + "月" + date1.get(Calendar.DAY_OF_MONTH) + "日");
		date1.setTimeInMillis(1234567898765L);
		System.out.println(date1.get(Calendar.YEAR) + "年" + date1.get(Calendar.MONTH) + "月"
				+ date1.get(Calendar.DAY_OF_MONTH) + "日");
	}
}

9.6 StopWatch类

package ChapterNine;

public class StopWatch {
	// 私有数据域
	private java.util.Date startTime;
	private java.util.Date endTime;

	// 无参构造方法
	public StopWatch() {
	}

	// start时间
	public void start() {
		this.startTime = new java.util.Date();
	}

	// stop时间
	public void stop() {
		this.endTime = new java.util.Date();
	}

	// 流逝时间之差
	public long getElapsedTime() {
		return endTime.getTime() - startTime.getTime();
	}
}

测试类

package ChapterNine;

public class StopWatchTest {
	public static void main(String[] args) {
		StopWatch sw = new StopWatch();
		sw.start();
		for (int i=1;i<=100000;i++) {
			for (int j=1;j<=100000;j++) {
			}
		}
		sw.stop();
		System.out.println(sw.getElapsedTime());
	}
}

9.7 Account类

package ChapterNine;

//Account类
public class Account {
	// 私有数据域
	private int id = 0;
	private double balance = 0;
	private double annualInterestRate = 0;
	private java.util.Date dateCreated;

	// 无参构造方法
	public Account() {
		dateCreated = new java.util.Date();
	}

	// 带了id和balance的构造方法
	public Account(int id, double balance) {
		dateCreated = new java.util.Date();
		this.id = id;
		this.balance = balance;
	}

	// id balance annualInterestRate的访问器和修改器
	public int getId() {
		return id;
	}

	public double getBalance() {
		return balance;
	}

	public double getAnnualInterestRate() {
		return annualInterestRate;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}

	public void setAnnualInterestRate(double annualInterestRate) {
		this.annualInterestRate = annualInterestRate;
	}

	// dateCreated访问器
	public String getDateCreated() {
		return dateCreated.toString();
	}

	// 月利率
	public double getMonthlyInterestRate() {
		return annualInterestRate / 12;
	}

	// 月利息
	public double getMonthlyInterest() {
		return balance * annualInterestRate / 12 / 100;
	}

	// 取钱
	public void withDraw(double money) {
		this.balance = this.balance - money;
	}

	// 存钱
	public void deposit(double money) {
		this.balance = this.balance + money;
	}
}

测试类

package ChapterNine;

public class AccountTest {
	public static void main(String[] args) {
		Account account1 = new Account(1122, 20000);
		account1.setAnnualInterestRate(4.5);
		account1.withDraw(2500);
		account1.deposit(3000);
		System.out.println("余额:" + account1.getBalance() + "\n月利息:" + account1.getMonthlyInterest() + "\n开户日期:"
				+ account1.getDateCreated());
	}
}

9.8 Fan类

package ChapterNine;

//风扇类
public class Fan {
	// 私有数据域
	private final int SLOW = 1, MEDIUM = 2, FAST = 3;
	private int speed = SLOW;
	private boolean on = false;
	private double radius = 5;
	private String color = "blue";

	// 无参构造方法
	public Fan() {

	}

	// 四个属于的访问器和修改器
	public int getSpeed() {
		return speed;
	}

	public boolean getOn() {
		return on;
	}

	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;
	}

	// toString方法
	public String toString() {
		String a = "风扇的速度:" + speed + "\n风扇的颜色:" + color + "\n风扇的半径:" + radius;
		String b = "fan is off" + "\n风扇的颜色:" + color + "\n风扇的半径:" + radius;
		if (on) {
			return a;
		} else {
			return b;
		}
	}
}

测试类

package ChapterNine;

public class FanTest {
	public static void main(String[] args) {
		Fan fan1 = new Fan();
		Fan fan2 = new Fan();

		fan1.setSpeed(3);
		fan1.setRadius(10);
		fan1.setColor("yellow");
		fan1.setOn(true);

		fan2.setSpeed(2);
		fan2.setRadius(5);
		fan2.setColor("blue");
		fan2.setOn(false);

		System.out.println(fan1.toString() + "\n" + fan2.toString());
	}
}

9.9 RegularPolygon类

package ChapterNine;

//正n边形类
public class RegularPolygon {
	// 私有数据域
	private int n = 3;
	private double side = 1;
	private double x = 0;
	private double y = 0;

	// 无参构造方法
	public RegularPolygon() {

	}

	// 带n和side的构造方法
	public RegularPolygon(int n, double side) {
		this.n = n;
		this.side = side;
	}

	// 带n、side、x、y的构造方法
	public 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 double getSide() {
		return side;
	}

	public double getX() {
		return x;
	}

	public double getY() {
		return y;
	}

	public void setN(int n) {
		this.n = n;
	}

	public void setSide(double side) {
		this.side = side;
	}

	public void setX(double x) {
		this.x = x;
	}

	public void setY(double y) {
		this.y = y;
	}

	// 获取周长的方法
	public double getPerimeter() {
		return n * side;
	}

	// 获取面积的方法
	public double getArea() {
		return (n * side * side) / (4 * Math.tan(Math.PI / n));
	}
}

测试类

package ChapterNine;

public class RegularPolygonTest {
	public static void main(String[] args) {
		RegularPolygon r1=new RegularPolygon();
		RegularPolygon r2=new RegularPolygon(6,4);
		RegularPolygon r3=new RegularPolygon(10,4,5.6,7.8);
		
		System.out.println("周长:"+r1.getPerimeter()+"\n面积:"+r1.getArea());
		System.out.println("周长:"+r2.getPerimeter()+"\n面积:"+r2.getArea());
		System.out.println("周长:"+r3.getPerimeter()+"\n面积:"+r3.getArea());
	}
}

9.10 QuadraticEquation类

package ChapterNine;

//二次方程式类
public class QuadraticEquation {
	// 私有数据域
	private double a, b, c;

	// 带有abc参数的构造方法
	public QuadraticEquation(double a, double b, double c) {
		this.a = a;
		this.b = b;
		this.c = c;
	}

	// abc的访问器
	public double getA() {
		return a;
	}

	public double getB() {
		return b;
	}

	public double getC() {
		return c;
	}

	// 判别式
	public double getDiscriminant() {
		return (b * b) - 4 * a * c;
	}

	// 第一个根
	public double getRoot1() {
		if (this.getDiscriminant() >= 0) {
			return (-b + this.getDiscriminant()) / 2 * a;
		} else {
			return 0;
		}
	}

	// 第二个根
	public double getRoot2() {
		if (this.getDiscriminant() >= 0) {
			return (-b - this.getDiscriminant()) / 2 * a;
		} else {
			return 0;
		}
	}

}

测试类

package ChapterNine;

import java.util.Scanner;

public class QuadraticEquationTest {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);

		System.out.println("请输入a,b,c的值:");
		double a = input.nextDouble();
		double b = input.nextDouble();
		double c = input.nextDouble();

		QuadraticEquation q = new QuadraticEquation(a, b, c);
		if (q.getDiscriminant() > 0) {
			System.out.println(q.getRoot1() + " " + q.getRoot2());
		} else if (q.getDiscriminant() == 0) {
			System.out.println(q.getRoot1());
		} else {
			System.out.println("The equation has no roots.");
		}
	}
}

9.11 LinearEquation类

package ChapterNine;

//线性方程类
public class LinearEquation {
	// 私有数据域
	private double a, b, c, d, e, f;

	// 包含参数构造方法
	public 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;
	}

	// ad-bc的做分母是否为0判别方法
	public boolean isSolvable() {
		if (a * d - b * c == 0) {
			return false;
		} else {
			return true;
		}
	}

	// x的获取方法
	public double getX() {
		return (e * d - b * f) / (a * d - b * c);
	}

	// y的获取方法
	public double getY() {
		return (a * f - e * c) / (a * d - b * c);
	}
}

测试类

package ChapterNine;

import java.util.Scanner;

public class LinearEquationTest {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);

		System.out.println("请输入abcdef的值:");
		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 le = new LinearEquation(a, b, c, d, e, f);
		if (!le.isSolvable()) {
			System.out.println("The equation has no solution.");
		} else {
			System.out.println("x=" + le.getX() + " y=" + le.getY());
		}
	}
}

9.12 Interesection(交点)

package ChapterNine;

import java.util.Scanner;

public class Interesection {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);

		System.out.print("请输入x1,y1的值:");
		double x1 = input.nextDouble();
		double y1 = input.nextDouble();
		System.out.print("请输入x2,y2的值:");
		double x2 = input.nextDouble();
		double y2 = input.nextDouble();
		System.out.print("请输入x3,y3的值:");
		double x3 = input.nextDouble();
		double y3 = input.nextDouble();
		System.out.print("请输入x4,y4的值:");
		double x4 = input.nextDouble();
		double y4 = input.nextDouble();

		double a = y2 - y1;
		double b = x1 - x2;
		double e = (x1 - x2) * y1 + (y2 - y1) * x1;
		double c = y4 - y3;
		double d = x3 - x4;
		double f = (x3 - x4) * y3 + (y4 - y3) * x3;

		LinearEquation le = new LinearEquation(a, b, c, d, e, f);
		System.out.println("交点坐标为(" + le.getX() + "," + le.getY() + ")");

	}
}

9.13 Location类

package ChapterNine;

public class Location {
	// 公共数据域
	public int row, column;
	public double maxValue;

	// 构造方法
	public Location() {

	}

	// 返回最大值方法
	public static Location locateLargest(double[][] a) {
		Location my = new Location();
		my.maxValue = a[0][0];
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[0].length; j++) {
				if (my.maxValue < a[i][j]) {
					my.maxValue = a[i][j];
					my.row = i;
					my.column = j;
				}
			}
		}
		return my;
	}
}

测试类

package ChapterNine;

import java.util.Scanner;

public class LocationTest {
	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 i = input.nextInt();
		int j = input.nextInt();
		
		System.out.println("Enter the array:");
		double array[][] = new double[i][j];
		for (int a = 0; a < i; a++) {
			for (int b = 0; b < j; b++) {
				array[a][b] = input.nextDouble();
			}
		}

		Location my = Location.locateLargest(array);

		System.out.println(
				"The location of the largest element is " + my.maxValue + " at (" + my.row + "," + my.column + ")");

	}

}

  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值