Java语言程序设计与数据结构(基础篇)课后练习题 第十一章 (一)

11.1

import java.util.Date;
import java.util.Scanner;

public class dishiyizhang {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner(System.in);
	System.out.println("Enter three sides: ");
	double s1 = input.nextDouble();
	double s2 = input.nextDouble();
	double s3 = input.nextDouble();
	System.out.println("Enter color: ");
	String str = input.next();
	System.out.println("Enter filled: ");
	boolean filled = input.nextBoolean();
	Triangle t = new Triangle(s1, s2, s3);
	t.setColor(str);
	t.setFilled(filled);
	System.out.println("The area:" + t.getArea());
	System.out.println("The perimeter:" + t.getPerimeter());
	System.out.println("The color:" + t.getColor());
	System.out.println("The filled:" + t.isFilled());
}

}

class GeometricObject {

private String color = "white";
private boolean filled;
private Date dateCreated;
public GeometricObject() {
	dateCreated = new Date();
}

public GeometricObject(String color, boolean filled) {
	dateCreated = new Date();
	this.color = color;
	this.filled = filled;
}

public String getColor() {
	return color;
}

public void setColor(String color) {
	this.color = color;
}

public boolean isFilled() {
	return filled;
}

public void setFilled(boolean filled) {
	this.filled = filled;
}

public Date getDateCreate() {
	return dateCreated;
}

public String toString() {
	return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled;
}

}

class Triangle extends GeometricObject {

private double s1;
private double s2;
private double s3;
public Triangle() {
	super();
	this.s1 = this.s2 = this.s3 = 1;
}

public Triangle(double s1, double s2, double s3) {
	super();
	this.s1 = s1;
	this.s2 = s2;
	this.s3 = s3;
}

public double getSide1() {
	return s1;
}

public double getSide2() {
	return s2;
}

public double getSide3() {
	return s3;
}

public double getArea() {
	double p = (s1 + s2 + s3) / 2;
	return Math.sqrt(p * (p - s1) * (p - s2) * (p - s3));
}

public double getPerimeter() {
	return s1 + s2 + s3;
}

public String toString() {
	return "Triangle: side1 = " + s1 + " side2 = " + s2 + " side3 = " + s3;
}

}

11.2

package dishiyizhang;
public class dishiyizhang{

public static void main(String[] args) {
	Student s = new Student(Student.ONE,"老刘","上海","123456789","13456@email");
	Employee e = new Employee("程序员",2300.0,"老张","北京","123456789","145662@email");
	Faculty f = new Faculty("程序员2",2300.0,"time","grade","老李","北京","123456789","145662@email");
	Staff sta = new Staff("程序员3",2300.0,"post","老周","北京","123456789","145662@email");
	System.out.println(s.toString());
	System.out.println(e.toString());
	System.out.println(f.toString());
	System.out.println(sta.toString());
}

}

class Person {

private String name;
private String address;
private String number;
private String Email;
protected Person() {
}

protected Person(String name, String address, String number, String Email) {
	this.name = name;
	this.address = address;
	this.number = number;
	this.Email = Email;
}

protected String getName() {
	return name;
}

protected void setName(String name) {
	this.name = name;
}

protected String getAddress() {
	return address;
}

protected void setAddress(String address) {
	this.address = address;
}

protected String getNumber() {
	return number;
}

protected void setNumber(String number) {
	this.number = number;
}

protected String getEmail() {
	return Email;
}

protected void setEmail(String Email) {
	this.Email = Email;
}

@Override
public String toString() {
	return "ObejectByPerson" + "Name:" + getName();
}

}

class Student extends Person {

public final static String ONE = "Freshman";
public final static String TWO = "Sophomore";
public final static String THREE = "Junior";
public final static String FOUR = "Senior";
private String StudentState;

public Student() {
	StudentState = ONE;
}

public Student(String StudentState, String name, String address, String number, String Email) {
	super(name, address, number, Email);
	this.StudentState = StudentState;
}

public String getStudentState() {
	return StudentState;
}

public void setStudentState(String StudentState) {
	this.StudentState = StudentState;
}

public String toStringOfAllInforation() {
	return "Student is inforation:Grade:" + getStudentState() + "\tName:" + getName() + "\tAddress:" + getAddress()
			+ "\tNumber:" + getNumber() + "\tEmail:" + getEmail();
}

@Override
public String toString() {
	return "ObjectByStudent-->" + "Name:" + getName();
}

}

class Employee extends Person {

private String Office;
private double salary;

protected Employee() {
}

protected Employee(String Office, double salary, String name, String address, String number, String Email) {
	super(name, address, number, Email);
	this.Office = Office;
	this.salary = salary;
}

protected String getOffice() {
	return Office;
}

protected void setOffice(String Office) {
	this.Office = Office;
}

protected double getSalary() {
	return salary;
}

protected void setSalary(double salary) {
	this.salary = salary;
}

@Override
public String toString() {
	return "ObjectByEmployee-->" + "Name:" + getName();
}

}

class Faculty extends Employee {

private String OfficeTime;
private String PostGrade;
public Faculty() {

}

public Faculty(String Office, double salary, String OfficeTime, String PostGrade, String name, String address,
		String number, String Email) {
	super(Office, salary, name, address, number, Email);
	this.OfficeTime = OfficeTime;
	this.PostGrade = PostGrade;
}

public String getOfficeTime() {
	return OfficeTime;
}

public void setOfficeTime(String OfficeTime) {
	this.OfficeTime = OfficeTime;
}

public String getPostGrade() {
	return PostGrade;
}

public void setPostGrade(String PostGrade) {
	this.PostGrade = PostGrade;
}

public String toStringOfAllInforation() {
	return "Faculty is inforation:PostGrade:" + getPostGrade() + "\tOfficeTime:" + getOfficeTime() + "\tSalary:"
			+ getSalary() + "Office:" + getOffice() + "\tName:" + getName() + "\tAddress:" + getAddress()
			+ "\tNumber:" + getNumber() + "\tEmail:" + getEmail();
}

@Override
public String toString() {
	return "ObjectByFaculty-->" + "Name:" + getName();
}

}

class Staff extends Employee {

private String Post;
public Staff() {
}

public Staff(String Office, double salary, String Post, String name, String address, String number, String Email) {
	super(Office, salary, name, address, number, Email);
	this.Post = Post;
}

public String getPost() {
	return Post;
}

public void setPost(String Post) {
	this.Post = Post;
}

public String toStringOfAllInforation() {
	return "Staff is inforation:Post:" + getPost() + "\tSalary:" + getSalary() + "Office:" + getOffice() + "\tName:"
			+ getName() + "\tAddress:" + getAddress() + "\tNumber:" + getNumber() + "\tEmail:" + getEmail();
}

@Override
public String toString() {
	return "ObjectByStaff-->" + "Name:" + getName();
}

}

11.3

import java.util.Date;

public class dishiyizhang {

public static void main(String[] args) {
	Account a = new Account(1, 2000);
	CheckingAccount c = new CheckingAccount(114, 514, 1919);
	Saving_Account s = new Saving_Account();
	System.out.println(a.toString() + "\n" + c.toString() + "\n" + s.toString());
}

}

class Account {

private int id;
private double balance;
private double Rate;
private Date dateCreated;
public Account() {
	id = 0;
	balance = 0;
	Rate = 0;
	dateCreated = new Date();
}

public Account(int i, double b) {
	id = i;
	balance = b;
	Rate = 0;
	dateCreated = new Date();
}

public int getId() {
	return id;
}

public void setId(int j) {
	id = j;
}

public double getBalance() {
	return balance;
}

public void setBalance(double j) {
	balance = j;
}

public double getAnnualInterestRate() {
	return Rate;
}

public void setAnnualInterestRate(double j) {
	Rate = j;
}

public Date getDateCreated() {
	return dateCreated;
}

public double getMonthlyInterestRate() {
	return Rate / 12;
}

public double getMonthlyInterest() {
	return Rate / 12 * balance;
}

public void withDraw(double m) {
	balance -= m;
}

public void deposit(double m) {
	balance += m;
}

}

class CheckingAccount extends Account {

private double overDraw;
public CheckingAccount() {
	super();
}

public CheckingAccount(int i, double b, double o) {
	super(i, b);
	overDraw = o;
}

}

class Saving_Account extends Account {

//直接继承Account

}

11.4

import java.util.ArrayList;
import java.util.Scanner;

public class dishiyizhang {

public static void main(String[] args) {
	System.out.print("Enter numbers (0 finished): ");
	Scanner input = new Scanner(System.in);
	ArrayList<Integer> list = new ArrayList<Integer>();
	int n = input.nextInt();
	while (n != 0) {
		list.add(n);
		n = input.nextInt();
	}
	System.out.println("The maximum is " + max(list));
}
public static Integer max(ArrayList<Integer> list) {
	if (list == null || list.size() == 0)
		return null;
	else
		return java.util.Collections.max(list);
}

}

11.5

清单10-6.,,,,,前面的课程清单都找不到。。。看题目挺简单的,就是换成ArrayList来存储。

11.6

因为之前circle等的类都删除了,这里就写了一个Date对象,想要返回其他的再去看看前面课程吧。

import java.util.ArrayList;
import java.util.Date;

public class dishiyizhang {

public static void main(String[] args) {
	ArrayList<Object> list = new ArrayList<>();
	Date d = new Date();
	list.add(d);
	for(Object o: list)
		System.out.println(o.toString());
}

}

11.7

import java.util.ArrayList;

public class dishiyizhang {

public static void main(String[] args) {
	ArrayList<Integer> list = new ArrayList<>();	
	list.add(1);
	list.add(2);
	list.add(3);
	list.add(4);
	shuffle(list);
		System.out.println(list.toString());
}
public static void shuffle(ArrayList<Integer> list){
    java.util.Collections.shuffle(list);
}

}

11.8

import java.util.ArrayList;
import java.util.Date;

public class dishiyizhang {

public static void main(String[] args) {
	Account a = new Account(1122, 1000, "George");
	a.deposit(1000, "First deposit");
	a.deposit(2000, "Second deposit");
	a.deposit(3000, "Third deposit");
	a.withDraw(1, "First withdraw");
	a.withDraw(2, "Sceond withdraw");
	a.withDraw(3, "Third withdraw");
	System.out.print("name :" + a.getName() + " rate: " + a.getAnnualInterestRate() + " balance: " + a.getBalance() + "\n");
	for (Transaction b : a.transactions) {
		System.out.println("date: " + b.date);
		System.out.println("type: " + b.type);
		System.out.println("amount: " + b.amount);
		System.out.println("new balance: " + b.balance);
		System.out.println("description: " + b.description);
	}
}

}

class Account {

private int id;
private double balance;
private double Rate;
private Date dateCreated;
private String name;
ArrayList<Transaction> transactions;
public Account() {
	id = 0;
	balance = 0;
	Rate = 0;
	dateCreated = new Date();
	transactions = new ArrayList<Transaction>();
}

public Account(int i, double b) {
	id = i;
	balance = b;
	Rate = 0;
	dateCreated = new Date();
	transactions = new ArrayList<Transaction>();
}

public Account(int i, double b, String n) {
	id = i;
	balance = b;
	Rate = 0;
	dateCreated = new Date();
	name = n;
	transactions = new ArrayList<Transaction>();
}

public int getId() {
	return id;
}

public void setId(int j) {
	id = j;
}

public double getBalance() {
	return balance;
}

public void setBalance(double j) {
	balance = j;
}

public double getAnnualInterestRate() {
	return Rate;
}

public void setAnnualInterestRate(double j) {
	Rate = j;
}

public Date getDateCreated() {
	return dateCreated;
}

public double getMonthlyInterestRate() {
	return Rate / 12;
}

public double getMonthlyInterest() {
	return Rate / 12 * balance;
}

public void withDraw(double m, String s) {
	balance -= m;
	transactions.add(new Transaction('W', m, balance, s));
}

public void deposit(double m, String s) {
	balance += m;
	transactions.add(new Transaction('D', m, balance, s));
}

public void setName(String n) {
	name = n;
}

public String getName() {
	return name;
}

}

class Transaction {

public Date date;
public char type;
public double amount;
public double balance;
public String description;
public Transaction(char c, double a, double b, String d) {
	date = new Date();
	type = c;
	amount = a;
	balance = b;
	description = d;
}

}

11.9

import java.util.ArrayList;
import java.util.Scanner;

public class dishiyizhang {

public static void main(String[] args) {
	System.out.print("Enter the array size n: ");
	Scanner input = new Scanner(System.in);
	int s = input.nextInt();
	int[][] mt = new int[s][s];
	ArrayList<Integer> rows = new ArrayList<>();
	ArrayList<Integer> columns = new ArrayList<>();
	for (int i = 0; i < s; i++) {
		for (int j = 0; j < s; j++)
			mt[i][j] = (int) (Math.random() * 2);
	}
	for (int i = 0; i < s; i++) {
		int sum = 0;
		for (int j = 0; j < s; j++) {
			if (mt[i][j] == 1)
				sum++;
		}
		rows.add(sum);
	}
	for (int j = 0; j < s; j++) {
		int sum = 0;
		for (int i = 0; i < s; i++) {
			if (mt[i][j] == 1)
				sum++;
		}
		columns.add(sum);
	}
	System.out.println("The random array is ");
	for (int i = 0; i < s; i++) {
		for (int j = 0; j < s; j++) {
			System.out.print(mt[i][j] + " ");
		}
		System.out.println();
	}
	System.out.println("The largest row index: " +java.util.Collections.max(rows));
	System.out.println("The largest column index: "+java.util.Collections.max(columns));
}

}

  • 27
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xupengboo

你的鼓励将是我创作最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值