11.1(三角形Triangle)
父类(左边) 和 子类(右边)继承关系,
如下图:
package p11;
public class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
public GeometricObject() {
dateCreated=new java.util.Date();
}
public GeometricObject(String color, boolean filled) {
dateCreated=new java.util.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 java.util.Date getDateCreated() {
return dateCreated;
}
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color+
" and filled: " + filled;
}
}
package p11;
public class Triangle extends GeometricObject {
private double side1 = 1.0;
private double side2 = 1.0;
private double side3 = 1.0;
public Triangle() {
}
//这是一个 重载 的构造方法
public Triangle(double side1, double side2,
double side3, String color, boolean filled) {
super(color,filled);//或者 调用父类中已有方法 ,如下
//setColor(color);
//setFilled(filled);
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 s=(side1+side2+side3)/2;
double area=Math.pow( s*(s-side1)*(s-side2)*(s-side3) , 0.5);
return area;
}
public double getPerimeter() {
return side1+side2+side3;
}
@Override
public String toString() {
return super.toString() + "\nArea is " + getArea();
}
}
package p11;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入三角形的三条边:");
double s1=input.nextInt();
double s2=input.nextInt();
double s3=input.nextInt();
System.out.print("请输入三角形的颜色: ");
String c1=input.next();
System.out.print("请输入一个boolean值(false:不填充;true:填充):");
boolean f1=input.nextBoolean();
input.close();
//创建对象
GeometricObject object=new Triangle(s1, s2, s3, c1, f1);
System.out.println(object.toString());
}
}
(11.5~11.14节)
11.2(Person、Student、Employee、Faculty 和 Staff类)
这个不难,我也就是在 受聘日期 那里“卡壳”了一下
总体的继承关系:
package p11;
public class Person {
private String name;
private String adress;
private String phone;
private String email;
public Person(String name, String adress, String phone, String email) {
this.name=name;
this.adress=adress;
this.phone=phone;
this.email=email;
}
public Person(String name) {
this.name=name;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress=adress;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone=phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email=email;
}
public String toString() {
return "The Person's name is "+name+"\nAdress is "+adress+
"\nPhone is "+phone+"\nEmail is "+email;
}
}
package p11;
public class Student extends Person {
private int classState;
public final static int FRESHMAN=1;//大一新生
public final static int SOPHOMORE=2;//大二学生
public final static int JUNIOR=3;//大三学生
public final static int SENIOR=4;//大四学生
public Student(int classState, String name, String adress, String phone, String email) {
super(name, adress, phone, email);
this.classState=classState;
}
public int getClassState() {
return classState;
}
public void setClassState(int classState) {
this.classState=classState;
}
@Override
public String toString() {
return "The Student's classState is "+classState+"\n"+super.toString();
}
}
package p11;
import java.util.Date;
import p10.MyDate;
public class Employee extends Person {
private String office;
private double salary;
private MyDate DateOfAppointment;//受聘日期
//构造方法
public Employee(String office, double salary, MyDate DateOfAppointment,
String name, String adress, String phone, String email) {
super(name, adress, phone, email);
this.DateOfAppointment=DateOfAppointment;
this.salary=salary;
this.office=office;
}
public String getOffice() {
return office;
}
public void setOffice(String office) {
this.office=office;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary=salary;
}
public String getDateOfAppointment() {
return DateOfAppointment.getYear()+"年"+DateOfAppointment.getMonth()
+"月"+DateOfAppointment.getDay()+"日";
}
@Override
public String toString() {
return "The Employee's dateOfAppointment is "+ getDateOfAppointment()
+"\nOffice is "+office+"\nSalary is "+salary+"\n"+super.toString();
}
}
package p11;
import p10.MyDate;
public class Faculty extends Employee {
private long officeHours;
private String rank;
//构造方法
public Faculty(long officeHours, String rank, String office, double salary, MyDate DateOfAppointment,
String name, String adress, String phone, String email) {
super(office, salary, DateOfAppointment, name, adress, phone, email);
this.officeHours=officeHours;
this.rank=rank;
}
public long getOfficeHours() {
return officeHours;
}
public void setOfficeHours(long officeHours) {
this.officeHours=officeHours;
}
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank=rank;
}
@Override
public String toString() {
return "The Faculty's officeHours is "+officeHours+"\nRank is "+rank+
"\n"+super.toString();
}
}
package p11;
import p10.MyDate;
public class Staff extends Employee {
private String title;
//构造方法
public Staff(String title, String office, double salary, MyDate DateOfAppointment, String name,
String adress, String phone, String email) {
super(office, salary, DateOfAppointment, name, adress, phone, email);
this.title=title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title=title;
}
@Override
public String toString() {
return "The Staff's title is "+title+"\n"+super.toString();
}
}
package p11;
import p10.MyDate;
public class Test2 {
public static void main(String[] args) {
Person person=new Person("王大狗","垃圾堆","123456789","[email protected]");
Person student=new Student(4,"王大狗","垃圾堆","123456789","[email protected]");
Person employee=new Employee("五星街办公室一号",5600,new MyDate(1996,12,17),"王大狗","垃圾堆","123456789","[email protected]");
Person faculty=new Faculty(8, "二等", "五星街办公室一号",5600,new MyDate(1996,12,17),"王大狗","垃圾堆","123456789","[email protected]");
Person staff=new Staff("优秀员工","五星街办公室一号",5600,new MyDate(1996,12,17),"王大狗","垃圾堆","123456789","[email protected]");
System.out.println(person.toString());
System.out.println();
System.out.println(student.toString());
System.out.println();
System.out.println(employee.toString());
System.out.println();
System.out.println(faculty.toString());
System.out.println();
System.out.println(staff.toString());
}
}
11.3(账户类Account的子类)
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 p11;
import p9.Account;
public class CheckingAccount extends Account{
private double OverDraftLimit;//限定透支额度
public CheckingAccount(int id, double balance, double OverdraftLimit) {
super(id, balance);
this.OverDraftLimit=OverDraftLimit;//可用额度
}
//
public double getOverDraftLimit() {
return OverDraftLimit;
}
public void setOverDraftLimit(double OverDraftLimit) {
this.OverDraftLimit=OverDraftLimit;
}
@Override
//取钱
public double withDraw(double withdraw) {
//可用额度不为0时
if(getOverDraftLimit() != 0) {
//自动提款机,小于100元不提取
if(this.balance<100) {
//移用,“可用额度”
this.balance = this.balance + this.OverDraftLimit;
//移用,“可用额度”后,系统自动将可用额度 “清零”,以后的可用额度就为 "0"
setOverDraftLimit(0);
return this.balance=this.balance-withdraw;
}
else {
//正常卡里有钱并且>100元时,不用 “可用额度”
return this.balance=this.balance-withdraw;
}
}
//当可用额度为0时
else if(getOverDraftLimit() == 0){
if(this.balance<100) {
return -1;//返回一个负数,表示不可取钱了!!!
}
else {
//正常卡里有钱并且>100元时,不用 “可用额度”
return this.balance=this.balance-withdraw;
}
}
return this.balance;
}
public String toString() {
return "The ID:"+id+"\nThe balance is "
+balance+"\n可用额度为:"+getOverDraftLimit();
}
}
package p11;
import p9.Account;
public class SavingAccount extends Account{
public SavingAccount(int id, double balance) {
super(id,balance);
}
@Override //取钱方法 不可透支
public double withDraw(double withdraw) {
//自动提款机 <100 元是不能够取出来的,我这里用-1表示
if(this.balance<100) {
return -1;
}
else {
return this.balance=this.balance-withdraw;
}
}
public String toString() {
return "The ID:"+id+"\nThe balance is "
+balance;
}
}
11.4(ArrayList 的最大元素)
package p11;
import java.util.ArrayList;
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入一个以0结尾的数值序列:");
//创建一个数组列表:该ArrayList对象可以用于存储 Integer整型数值
ArrayList list=new ArrayList<>();
int value=input.nextInt();
while(value != 0) {
list.add(value);
value=input.nextInt();
}
input.close();
//调用max方法,来返回输入的最大值
System.out.println("最大值是:"+max(list));
}
//一个返回数组列表中最大值的方法
public static Integer max(ArrayList list) {
if(list.isEmpty() || list.size()==0) {
return null;
}
else {
int max=list.get(0);
int maxIndex=0;
for(int i=1; i
11.5(课程类 Course)
package p11;
import java.util.ArrayList;
public class Course {
private String courseName;
//用数组列表存储学生名
private ArrayList students = new ArrayList<>();
private int numberOfStudents;
//课程的一个构造方法
public Course(String courseName) {
this.courseName = courseName;
}
//返回课程名
public String getCourseName() {
return courseName;
}
//增加学生名字
public void addStudent(String student) {
students.add(student);
}
//删除学生名字
public void dropStudent(String student) {
students.remove(student);
}
//返回 学生名字列表
public ArrayList getStudents() {
return students;
}
//返回 学生人数
public int getNumberOfStudents() {
return numberOfStudents=students.size();
}
}
package p11;
public class Test5 {
public static void main(String[] args) {
Course object1=new Course("Date Structures");
Course object2=new Course("Date Systems");
object1.addStudent("Peter Jones");
object1.addStudent("Kim Smith");
object1.addStudent("Anne Kennedy");
object2.addStudent("Peter Jones");
object2.addStudent("Steve Smith");
System.out.println("Number of students in course1: "
+ object1.getNumberOfStudents());
//输出 数组列表
System.out.println(object1.getStudents());
System.out.println("Number of students in course2: "
+object2.getNumberOfStudents());
}
}
11.6(使用 ArrayList)
说明一下:我没有创建Loan类、Circle类,而是直接用我自己之前包里有的类来创建对象,其实用哪个类都是一样的。
package p11;
import java.util.ArrayList;
import java.util.Date;
public class Test6 {
public static void main(String[] args) {
ArrayList
11.7(打乱ArrayList)
package p11;
import java.util.ArrayList;
import java.util.Arrays;
public class Test7 {
public static void main(String[] args) {
Integer[] array= {3, 5, 95, 4, 15, 34, 3, 6, 5};
ArrayList list=new ArrayList<>(Arrays.asList(array));
shuffle(list);
}
public static void shuffle(ArrayList list) {
java.util.Collections.shuffle(list);
System.out.println(list);
}
}
**11.8(新的 Account 类)
package p11;
import java.util.ArrayList;
public class Test8 {
public static void main(String[] args) {
//用于为账户存储交易
//ArrayList
package p11;
import java.util.ArrayList;
import java.util.Date;
public class NewAccount {
private String name;
//用于为账户存储交易
private ArrayList
package p11;
import java.util.Date;
public class Transaction {
//交易日期
private java.util.Date date;
//交易类型,例如'W','D'
private char type;
//交易量
private double amount;
//交易后的新余额
private double balance;
//交易描述
private String description;
//一个有参构造方法
public Transaction(char type, double amount, double balance, String description) {
this.type=type;
this.amount=amount;
this.balance=balance;
this.description=description;
//创建一个交易日期
date=new Date();
this.date=date;
}
//获得交易日期
public String getDate() {
return date.toString();
}
//设置交易日期
public void setDate(long eclapseTime) {
date.setTime(eclapseTime);
}
//交易类型:‘W’——取款,‘D’——存款
public char getType() {
return type;
}
public void setType(char type) {
this.type=type;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount=amount;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance=balance;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description=description;
}
}
*11.9(最大的行和列)
注意:本人感觉我这代码还不够完善,比如,测试完会有这种[【0,0,0,1,2,3】的下标出现,其实代表的也就是【0,1,2】。
当然也会出现这种样子的运行结果:
package p11;
import java.util.ArrayList;
import java.util.Scanner;
public class Test9 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter the array size n: ");
int n=input.nextInt();
input.close();
System.out.println("The random array is : ");
//创建一个二维数组
int[][] array=new int[n][n];
//下标[row]:行
//下标[column]:列
//按行,循环赋值,并且输出
for(int row=0; row list1=new ArrayList<>();
int maxRow1=0;//"行最大值"——(一行的元素相加得到的最大值)
int maxIndex1=0;//"行最大值的下标 "——(若某一行具有最大值,则将其下标赋值给maxIndex)
//先计算第一行的和,并且先让它的和为最大值,它的下标为最大值下标。
//然后拿这个最大值 与 剩下的行的各自的和相比较,找出真正的最大值和最大值下标
for(int column=0; column list2=new ArrayList<>();
int maxColumn=0;
int MaxIndex=0;
for(int row=0; row
11.10(利用继承实现MyStack)
package p11;
import java.util.ArrayList;
public class MyStack extends ArrayList{
private ArrayList
package p11;
import java.util.Scanner;
public class Test10 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入5个字符串: ");
String s1=input.nextLine();
String s2=input.nextLine();
String s3=input.nextLine();
String s4=input.nextLine();
String s5=input.nextLine();
input.close();
//创建一个栈类
MyStack stack=new MyStack();
//入栈
stack.push(s1);
stack.push(s2);
stack.push(s3);
stack.push(s4);
stack.push(s5);
//输出:可以直接用toString()——不过这是给你看一个入栈效果,它是顺序的
System.out.println(stack.toString());
//换行
System.out.println();
//逆序输出:可以用出栈 pop():先进后出
int leng=stack.getSize();
for(int i=0; i
11.11(对ArrayList排序)
package p11;
import java.util.ArrayList;
import java.util.Scanner;
public class Test11 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入5个数字: ");
int number1=input.nextInt();
int number2=input.nextInt();
int number3=input.nextInt();
int number4=input.nextInt();
int number5=input.nextInt();
input.close();
ArrayList list=new ArrayList<>();
list.add(number1);
list.add(number2);
list.add(number3);
list.add(number4);
list.add(number5);
sort(list);
System.out.println(list);
}
//对一个数值的ArrayList进行升序排序(从小到大排序)
public static void sort(ArrayList list) {
java.util.Collections.sort(list);
}
}
11.12(对ArrayList求和)
package p11;
import java.util.ArrayList;
import java.util.Scanner;
public class Test12 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入5个数字: ");
double number1=input.nextDouble();
double number2=input.nextDouble();
double number3=input.nextDouble();
double number4=input.nextDouble();
double number5=input.nextDouble();
input.close();
ArrayList list=new ArrayList<>();
list.add(number1);
list.add(number2);
list.add(number3);
list.add(number4);
list.add(number5);
System.out.println("它们的和为: " + sum(list));
}
public static double sum(ArrayList list) {
double sum=0;
int leng=list.size();
for(int i=0; i
*11.13(去掉重复元素)
有两种输出方式给你随意选择
package p11;
import java.util.ArrayList;
import java.util.Scanner;
public class Test13 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
//创建一个数组 list
ArrayList list=new ArrayList<>();
System.out.print("Enter ten integers: ");
int n1=input.nextInt();
int n2=input.nextInt();
int n3=input.nextInt();
int n4=input.nextInt();
int n5=input.nextInt();
int n6=input.nextInt();
int n7=input.nextInt();
int n8=input.nextInt();
int n9=input.nextInt();
int n10=input.nextInt();
input.close();
list.add(n1);
list.add(n2);
list.add(n3);
list.add(n4);
list.add(n5);
list.add(n6);
list.add(n7);
list.add(n8);
list.add(n9);
list.add(n10);
//去除重复的数字
removeDuplicate(list);
}
public static void removeDuplicate(ArrayList list) {
ArrayList newList=new ArrayList<>();
for(int i=0; i
11.14(结合两个列表)
package p11;
import java.util.ArrayList;
import java.util.Scanner;
public class Test14 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter five integers for list1: ");
int t1;
ArrayList list1=new ArrayList<>();
for(int i=0; i<5; i++) {
t1=input.nextInt();
list1.add(t1);
}
System.out.print("Enter five integers for list2: ");
int t2;
ArrayList list2=new ArrayList<>();
for(int i=0; i<5; i++) {
t2=input.nextInt();
list2.add(t2);
}
union(list1,list2);
}
public static void union(ArrayList list1, ArrayList list2) {
//将list2加在list1后面
list1.addAll(list2);
System.out.print("The combined list is ");
for(int i=0; i
*11.15(凸多边形面积)
//这题不太懂面积怎么求,你们可以去百度看看别的大神咋弄得
我有带更新。。。。
我先做下一题,(#^.^#)嘻嘻~
**11.16(加法测试)
package p11;
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);
//创建一个数组,用来存放错误成绩
ArrayList list=new ArrayList<>();
Scanner input=new Scanner(System.in);
System.out.print("What is "+number1+" + "+number2+"?");
int answer=input.nextInt();
while(number1+number2!=answer) {
if( !list.contains(answer) ) {
list.add(answer);
}
else {
System.out.println("You already entered "+answer);
}
System.out.print("Wrong answer.Try again.What is "
+ number1+" + "+number2+"? ");
answer=input.nextInt();
}
System.out.println("You got it!");
}
}
**11.17(代数:完全平方)
这一题其实不难,分为两大步骤走:(两大要点,基本掌握,你自己都会编程了)
一、知道怎样从一个整数中分离出它的因子(在第五单元的练习题(5.16题)中做过)
二、知道怎样统计一个列表中出现重复元素的个数(其实这个和消除重复的过程有点相似)
package p11;
import java.util.ArrayList;
import java.util.Scanner;
public class Test17 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
//创建一个数组列表来保存“因子”
ArrayList list=new ArrayList<>();
System.out.print("Enter an integer m: ");
//-------------------------------------------------------------
//源代码在我之前做过的练习里出现过。
//第五单元 编程练习题 5.16(找出一个整数的因子)题中
int number=input.nextInt();
input.close();
//得到 M
int m=number;
int i=2;
System.out.print("因子为: ");
while(number != 1) {
if(number % i ==0) {
number = number / i ;
System.out.print( i + " ");//输出因子
list.add(i);
}
else {
i++;
}
}
System.out.println();
System.out.println("检查 list数组里的情况: "+list.toString());
//--------------------------------------------------------------------
ArrayList newList=new ArrayList<>();
int[] countArray=new int[1000];
for(int j=0; j