Java语言程序设计与数据结构(基础篇)第11章 课后练习题
作者:互联网
文章目录
Java语言程序设计与数据结构(基础篇)第11章 课后练习题
Test1
package com.programList;
public class Triangle extends GeometricObject {
private double side1;
private double side2;
private double side3;
public Triangle() {
this(1.0, 1.0, 1.0);
}
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
public double getArea() {
double p = (side1 + side2 + side3) / 2;
return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
}
public double getPerimeter() {
return side1 + side2 + side3;
}
public String toString() {
return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;
}
}
Test2
package com.programList1;
import java.util.Date;
public class Employee extends Person {
private String office;
private String wage;
private Date dateCreated;
public Employee() {
office = "";
wage = "";
dateCreated = new Date();
}
public Employee(String name, String address, String phoneNumber, String mailAddress, String office, String wage, Date dateCreated) {
super(name, address, phoneNumber, mailAddress);
this.office = office;
this.wage = wage;
this.dateCreated = dateCreated;
}
public String getOffice() {
return office;
}
public String getWage() {
return wage;
}
public Date getDateCreated() {
return dateCreated;
}
public void setOffice(String office) {
this.office = office;
}
public void setWage(String wage) {
this.wage = wage;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
@Override
public String toString(){
return "ClassEmployee name: " + getName();
}
}
package com.programList1;
import java.util.Date;
public class Faculty extends Employee {
private String officeTime;
private String level;
public Faculty() {
}
public Faculty(String name, String address, String phoneNumber, String mailAddress, String office, String wage, Date dateCreated, String officeTime, String level) {
super(name, address, phoneNumber, mailAddress, office, wage, dateCreated);
this.officeTime = officeTime;
this.level = level;
}
public String getOfficeTime() {
return officeTime;
}
public String getLevel() {
return level;
}
public void setOfficeTime(String officeTime) {
this.officeTime = officeTime;
}
public void setLevel(String level) {
this.level = level;
}
@Override
public String toString() {
return "ClassFaculty name: " + getName();
}
}
package com.programList1;
public class Person {
private String name;
private String address;
private String phoneNumber;
private String mailAddress;
public Person() {
}
public Person(String name, String address, String phoneNumber, String mailAddress) {
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
this.mailAddress = mailAddress;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getMailAddress() {
return mailAddress;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public void setMailAddress(String mailAddress) {
this.mailAddress = mailAddress;
}
@Override
public String toString() {
return "ClassPerson name:" + getName();
}
}
package com.programList1;
import java.util.Date;
public class Staff extends Employee {
private String title;
public Staff() {
}
public Staff(String name, String address, String phoneNumber, String mailAddress, String office, String wage, Date dateCreated, String title) {
super(name, address, phoneNumber, mailAddress, office, wage, dateCreated);
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "ClassStaff name: " + getName();
}
public static void main(String[] args) {
Person person = new Person();
person.setName("张三丰");
person.setPhoneNumber("000000");
person.setAddress("zhangsanfeng");
person.setMailAddress("zhangsanfeng@mail.com");
System.out.println(person.toString());
Student student = new Student();
student.setName("张翠山");
student.setPhoneNumber("000001");
student.setAddress("zhangcuishan");
student.setMailAddress("zhangcuishan@mail.com");
student.setGrade(3);
System.out.println(student.toString());
Employee employee = new Employee("张无忌","zhangwuji","000002","zhangwuji@mail.com","wudangshan","10000",new Date());
System.out.println(employee.toString());
Faculty faculty = new Faculty("赵敏","zhaomin","000003","zhaomin@mail.com","yuanchao","10000",new Date(),"9-17","2");
System.out.println(faculty.toString());
Staff staff = new Staff("周芷若","zhouzhiruo","000004","zhouzhiruo@mail.com","峨眉山","1000000",new Date(),"掌门人");
System.out.println(staff.toString());
}
}
package com.programList1;
public class Student extends Person {
public static final int Freshman = 1;
public static final int Sophomore = 2;
public static final int Junior = 3;
public static final int Senior = 4;
private int grade;
public Student() {
grade = Freshman;
}
public Student(String name, String address, String phoneNumber, String mailAddress, int grade) {
super(name, address, phoneNumber, mailAddress);
this.grade = grade;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
@Override
public String toString() {
return "ClassStudent name:" + getName();
}
}
Test3
package com.programList;
import java.util.Date;
public class Account {
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
public Account() {
id = 0;
balance = 0;
annualInterestRate = 0;
dateCreated = new Date();
}
public Account(int id, double balance) {
this.id = id;
this.balance = balance;
annualInterestRate = 0;
dateCreated = new Date();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public Date getDateCreated() {
return dateCreated;
}
public double getMonthlyInterestRate() {
return annualInterestRate / 12;
}
public double getMonthlyInterest() {
return annualInterestRate / 12 * balance;
}
public void withDraw(double draw) {
this.balance -= draw;
}
public void deposit(double deposit) {
this.balance += deposit;
}
public static void main(String[] args) {
Account account = new Account(1122,2000);
account.setAnnualInterestRate(4.5/100);
account.withDraw(2500);
account.deposit(3000);
System.out.println("Balance: "+account.getBalance());
System.out.println("Monthly Interest: "+account.getMonthlyInterest());
System.out.println("Register Date: "+account.getDateCreated().toString());
}
}
package com.programList;
public class CheckingAccount extends Account{
private double checklevel;
public CheckingAccount() {
}
public CheckingAccount(int id, double balance, double checklevel) {
super(id, balance);
this.checklevel = checklevel;
}
public double getChecklevel() {
return checklevel;
}
public void setChecklevel(double checklevel) {
this.checklevel = checklevel;
}
}
package com.programList;
public class SavingAccount extends Account{
//直接继承Account即可
}
Test4
package com.programList;
import java.util.ArrayList;
public class Test4 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(2);
list.add(8);
list.add(0);
list.add(-2);
list.add(2);
System.out.println(max(list));
}
public static Integer max(ArrayList<Integer> list) {
if (list.size() == 0)
return 0;
Integer max = list.get(0);
for (int i = 1; i < list.size(); i++) {
if (list.get(i) > max) {
max = list.get(i);
}
}
return max;
}
}
Test5
package com.programList;
import java.util.ArrayList;
public class Course {
//课程名字
private String courseName;
//课程学生
private ArrayList<String> students = new ArrayList<>();
private int numberOfStudents;
public Course(String courseName) {
this.courseName = courseName;
}
public void addStudent(String student) {
students.add(student);
numberOfStudents++;
}
public ArrayList<String> getStudents() {
return students;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public String getCourseName() {
return courseName;
}
public void dropStudent(String student) {
students.remove(student);
numberOfStudents--;
}
public void clear() {
this.students = new ArrayList<>();
this.numberOfStudents = 0;
}
public static void main(String[] args) {
Course course = new Course("math");
course.addStudent("张三丰");
course.addStudent("张翠山");
course.addStudent("张无忌");
System.out.println(course.getNumberOfStudents());
course.dropStudent("张翠山");
System.out.println(course.getNumberOfStudents());
System.out.println(course.getStudents().get(0));
System.out.println(course.getStudents().get(1));
}
}
Test6
package com.programList;
import java.util.Date;
public class Loan {
private double annualInterestRate; //贷款年利率
private int numberOfYears; //贷款年数
private double loanAmount; //贷款额度
private Date loanDate; //创建贷款的日期
//无参构造方法,默认贷款年利率2.5,贷款年限为1年,贷款额度为1000
public Loan() {
this(2.5, 1, 1000);
}
//带参构造方法
public Loan(double annualInterestRate, int numberOfYears, double loanAmount) {
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
loanDate = new Date();
}
//返回年利率
public double getAnnualInterestRate() {
return annualInterestRate;
}
//设置年利率
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
//返回贷款年限
public int getNumberOfYears() {
return numberOfYears;
}
//设置贷款年限
public void setNumberOfYears(int numberOfYears) {
this.numberOfYears = numberOfYears;
}
//返回贷款额度
public double getLoanAmount() {
return loanAmount;
}
//设置贷款额度
public void setLoanAmount(double loanAmount) {
this.loanAmount = loanAmount;
}
//计算月支付额度
public double getMonthlyPayment() {
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)));
return monthlyPayment;
}
//计算总支付额度
public double getTotalPayment() {
double totalPayment = getMonthlyPayment() * numberOfYears * 12;
return totalPayment;
}
//返回贷款创建引用
public Date getLoanDate() {
return loanDate;
}
@Override
public String toString(){
return "created on " + getLoanDate().toString() + "\nannualInterestRate: " + getAnnualInterestRate() + "\nnumberOfYears: " + getNumberOfYears() +"\nloanAmount: " + getLoanAmount();
}
}
package com.programList;
public class Circle extends GeometricObject {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public Circle(double radius, String color, boolean filled) {
this.radius = radius;
setColor(color); //父类的方法,不能使用this.color = color,因为GeometricObject类中的私有数据域color和filled是不能被其他类访问
setFilled(filled); //父类的方法,不能使用this.filled = filled,因为GeometricObject类中的私有数据域color和filled是不能被其他类访问
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return radius * radius * Math.PI;
}
public double getDiameter() {
return 2 * radius;
}
public double getPerimeter() {
return 2 * radius * Math.PI;
}
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() + " and the radius is " + radius);
}
public String toString() {
return super.toString() + "\nradius is " + radius;
}
}
package com.programList;
import java.util.ArrayList;
import java.util.Date;
public class Test6 {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add(new Loan());
list.add(new Date());
list.add("字符串");
list.add(new Circle(5));
for (Object list1 : list){
System.out.println(list1.toString());
System.out.println();
}
}
}
Test7
package com.programList;
import java.util.ArrayList;
import java.util.Collections;
public class Test7 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(0);
list.add(1);
list.add(2);
list.add(3);
shuffle(list);
for (Integer element : list)
System.out.println(element);
}
public static void shuffle(ArrayList<Integer> list){
Collections.shuffle(list);
}
}
Test8
package com.programList1;
import java.util.ArrayList;
import java.util.Date;
public class Account {
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
private String name;
private ArrayList<Transcation> transcations;
public Account() {
id = 0;
balance = 0;
annualInterestRate = 0;
dateCreated = new Date();
transcations = new ArrayList<>();
}
public Account(int id, double balance) {
this.id = id;
this.balance = balance;
annualInterestRate = 0;
dateCreated = new Date();
transcations = new ArrayList<>();
}
public Account(int id, double balance, String name) {
this.id = id;
this.balance = balance;
this.name = name;
annualInterestRate = 0;
dateCreated = new Date();
transcations = new ArrayList<>();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setName(String name) {
this.name = name;
}
public void setTranscations(ArrayList<Transcation> transcations) {
this.transcations = transcations;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public Date getDateCreated() {
return dateCreated;
}
public String getName() {
return name;
}
public ArrayList<Transcation> getTranscations() {
return transcations;
}
public double getMonthlyInterestRate() {
return annualInterestRate / 12;
}
public double getMonthlyInterest() {
return annualInterestRate / 12 * balance;
}
public void withDraw(double draw,String description) {
this.balance -= draw;
transcations.add(new Transcation(new Date(),'W',draw,balance,description));
}
public void deposit(double deposit,String description) {
this.balance += deposit;
transcations.add(new Transcation(new Date(),'D',deposit,balance,description));
}
public static void main(String[] args) {
Account account = new Account(1122,2000);
account.setAnnualInterestRate(4.5/100);
account.withDraw(2500,"取钱");
account.deposit(3000,"存钱");
System.out.println("Balance: "+account.getBalance());
System.out.println("Monthly Interest: "+account.getMonthlyInterest());
System.out.println("Register Date: "+account.getDateCreated().toString());
}
}
Test9
package com.programList;
import java.util.ArrayList;
import java.util.Scanner;
public class Test9 {
public static void main(String[] args) {
System.out.print("Enter the array size n: ");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[][] matrix = new int[n][n];
ArrayList<Integer> rows = new ArrayList<>();
ArrayList<Integer> columns = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
matrix[i][j] = (int) (Math.random() * 2);
}
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 1)
sum++;
}
}
for (int j = 0; j < n; j++) {
int sum = 0;
for (int i = 0; i < n; i++) {
if (matrix[i][j] == 1)
sum++;
}
columns.add(sum);
}
System.out.println("The random array is ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(matrix[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));
}
}
Test10
package com.programList1;
import java.util.ArrayList;
public class MyStack extends ArrayList {
public boolean isEmpty() {
return super.isEmpty();
}
public int getSize() {
return super.size();
}
public Object peek() {
return super.get(getSize() - 1);
}
public Object pop(){
Object o = peek();
super.remove(o);
return o;
}
public void push(Object o){
super.add(o);
}
@Override
public String toString(){
return "stack: " + super.toString();
}
public static void main(String[] args) {
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
System.out.println(myStack.peek());
myStack.pop();
System.out.println(myStack.getSize());
}
}
Test11
package com.programList;
import java.util.ArrayList;
import java.util.Scanner;
public class Test11 {
public static void main(String[] args) {
System.out.print("Enter 5 integers: ");
Scanner input = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Integer n = input.nextInt();
list.add(n);
}
sort(list);
for (int i = 0; i < 5; i++)
System.out.println(list.get(i));
}
public static void sort(ArrayList<Integer> list) {
java.util.Collections.sort(list);
}
}
Test12
package com.programList;
import java.util.ArrayList;
import java.util.Scanner;
public class Test12 {
public static void main(String[] args) {
System.out.print("Enter 5 doubles: ");
Scanner input = new Scanner(System.in);
ArrayList<Double> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
double n = input.nextDouble();
list.add(n);
}
double sum = sum(list);
System.out.println("The sum is " + sum(list));
}
public static double sum(ArrayList<Double> list) {
double n1 = 0;
for (double n2 : list)
n1 += n2;
return n1;
}
}
Test13
package com.programList;
import java.util.ArrayList;
import java.util.Scanner;
public class Test13 {
public static void main(String[] args) {
System.out.print("Enter 10 integers: ");
Scanner input = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
int n = input.nextInt();
list.add(n);
}
removeDuplicate(list);
System.out.print("The distinct integers are ");
for (Integer integer : list)
System.out.print(integer + " ");
}
public static void removeDuplicate(ArrayList<Integer> list) {
int index = 0;
while (index < list.size()) {
int number = list.get(index);
if (list.indexOf(number) != index)
list.remove(index);//number在下标index之前已经出现过
else
index++;
}
}
}
Test14
package com.programList;
import java.util.ArrayList;
import java.util.Scanner;
public class Test14 {
public static void main(String[] args) {
System.out.print("Enter five integers for list1: ");
Scanner input = new Scanner(System.in);
ArrayList<Integer> list1 = new ArrayList<>();
for (int i = 0; i < 5; i++) {
int n = input.nextInt();
list1.add(n);
}
System.out.print("Enter five integers for list2: ");
ArrayList<Integer> list2 = new ArrayList<>();
for (int i = 0; i < 5; i++) {
int n = input.nextInt();
list2.add(n);
}
ArrayList<Integer> list = union(list1, list2);
System.out.print("The combined list is ");
for (Integer i : list)
System.out.print(i + " ");
}
public static ArrayList<Integer> union(ArrayList<Integer> list1, ArrayList<Integer> list2) {
ArrayList<Integer> list = new ArrayList<>();
list.addAll(list1);
list.addAll(list2);
return list;
}
}
Test15
略
Test16
package com.programList;
import java.util.ArrayList;
import java.util.Scanner;
public class Test16 {
public static void main(String[] args) {
int number1 = (int) (Math.random() * 10);
int number2 = (int) (Math.random() * 10);
Scanner input = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
System.out.print("What is " + number1 + " + " + number2 + "? ");
int answer = input.nextInt();
list.add(answer);
while (number1 + number2 != answer) {
System.out.print("Wrong answer. Try again. What is " + number1 + " + " + number2 + "? ");
answer = input.nextInt();
list.add(answer);
if (list.indexOf(answer) != -1) {
System.out.println("You already entered " + answer);
}
}
System.out.println("You got it!");
}
}
Test17
package com.programList;
import java.util.ArrayList;
import java.util.Scanner;
public class Test17 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer m:");
int m = input.nextInt();
int mTemp = m;
ArrayList<Integer> list = new ArrayList<>();
for (int i = 2; i <= m; ) {
if (m % i == 0 && isPrime(i)) {
list.add(i);
m /= i;
i = 2;
} else {
i++;
}
}
int[] divisor = new int[list.get(list.size() - 1) + 1];
for (int i = 0; i < list.size(); i++) {
divisor[list.get(i)]++;
}
int n = 1;
for (int i = 0; i < divisor.length; i++) {
if (divisor[i] % 2 == 1) {
n *= i;
}
}
System.out.println("The smallest number n for m * n to be a perfect square is " + n);
System.out.println("m * n is " + (mTemp * n));
}
public static boolean isPrime(int number) {
for (int divisor = 2; divisor <= number / 2; divisor++) {
if (number % divisor == 0) {
return false;
}
}
return true;
}
}
Test18
package com.programList;
import java.util.ArrayList;
public class Test18 {
public static void main(String[] args) {
ArrayList<Character> list = toCharacterArray("abc");
for (int i : list)
System.out.println((char) i);
}
public static ArrayList<Character> toCharacterArray(String s) {
ArrayList<Character> list = new ArrayList<>();
for (int i = 0; i < s.length(); i++)
list.add(s.charAt(i));
return list;
}
}
Test19
package com.programList;
import java.util.ArrayList;
import java.util.Scanner;
public class Test19 {
public static void main(String[] args) {
ArrayList<Container> list = new ArrayList<>();
list.add(new Container());
System.out.print("Enter the number of the objects: ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
System.out.print("Enter the weights of the objects: ");
for (int i = 0; i < num; i++) {
double n = input.nextDouble();
boolean in = false;
for (Container list2 : list) {
if (list2.w >= n) {
list2.list.add(n);
list2.w -= n;
in = true;
break;
}
}
if (!in) {
Container list3 = new Container();
list3.w -= n;
list3.list.add(n);
list.add(list3);
}
}
int index = 1;
for (Container list4 : list) {
System.out.print("Container " + index + " contains objects with weight ");
index++;
for (double i : list4.list)
System.out.printf("%.0f ", i);
System.out.println();
}
}
}
class Container {
public double w = 10;
public ArrayList<Double> list;
public Container() {
list = new ArrayList<>();
}
}
标签:11,Java,String,int,double,ArrayList,list,课后练习,public 来源: https://blog.csdn.net/weixin_50850749/article/details/117266495