山东大学Java程序设计课程实验报告5设计和使用类
2017-11-24 23:41
519 查看
实验目的:
1.掌握定义类、创建对象、使用类与对象。
2.掌握类及成员的修饰符的使用。
3.掌握构造函数的使用。
4.掌握如何定义方法和调用方法。
5.掌握形式参数与实际参数的结合过程。
实验步骤与内容:
1. 构建书和书包类,完成买书的过程。
2. 构建人类 ,尽可能的列举多个属性。姓名、性别、年龄、生日、身份证等等,并注意各个属性之间的联系。
3. 写一个程序,将以上三个类放在一起。人背着书包去买书。
package experiment5;
import java.text.NumberFormat;
import java.util.Scanner;
public class together {
public static void main (String []args) {
Person o1;Bag bag=new Bag();
o1=new Person();
o1.putMessage();
o1.getID();
o1.countAge();
BookStore books=new BookStore();
books.addbook("My", 18.00);
books.addbook("Heart", 9.99);
books.addbook("Will", 32.50);
books.addbook("Go", 23.50);
books.addbook("On", 100.00);
System.out.println(books);
int i=0;
String choice = "y";
double total=0;
NumberFormat fmt=NumberFormat.getCurrencyInstance();
System.out.println("请选择合适的书包大小");
String[] Bag = bag.chooseBag();
while (choice.equalsIgnoreCase("y")) {
System.out.println("请输入你想买的书");
Scanner scan = new Scanner(System.in);
Bag[i]=scan.nextLine();
System.out.println(fmt.format(books.getPrice(Bag[i])));
total+=books.getPrice(Bag[i]);
i++;
System.out.println("还想继续买吗?Y是N否");
choice = scan.nextLine();}
if (choice.equalsIgnoreCase("n")) {
o1.printMessage();
System.out.println("您现在包里装了"+i+"本书,价格总计:"+fmt.format(total));
}
}
}
1.掌握定义类、创建对象、使用类与对象。
2.掌握类及成员的修饰符的使用。
3.掌握构造函数的使用。
4.掌握如何定义方法和调用方法。
5.掌握形式参数与实际参数的结合过程。
实验步骤与内容:
1. 构建书和书包类,完成买书的过程。
2. 构建人类 ,尽可能的列举多个属性。姓名、性别、年龄、生日、身份证等等,并注意各个属性之间的联系。
3. 写一个程序,将以上三个类放在一起。人背着书包去买书。
package experiment5; import java.util.Scanner; import java.text.NumberFormat; public class Bag { int hasbook=0; Scanner scan= new Scanner(System.in); public Bag() { } public String[] chooseBag() { int i =scan.nextInt(); String []bag=new String[i]; return bag; } public int hasbook() { for( int i=0; i<chooseBag().length; i++) if(chooseBag()[i]!=null) hasbook++; return hasbook; } }
package experiment5; import java.text.NumberFormat; public class Book { public String name; private double price; public Book(String name,double price) { this.name=name; this.price=price; } public double getPrice() { return price; } public String getName() { return name; } public String toString() { NumberFormat fmt = NumberFormat.getCurrencyInstance(); String book; book="name:"+name+" price"+fmt.format(price); return book; } }
package experiment5; import java.text.NumberFormat; public class BookStore { private Book[] collection; private int count; private double cost; public BookStore() { collection = new Book[20]; count = 0; cost = 0.0; } public void addbook(String name, double price) { collection[count] = new Book(name, price); count++; } public double getPrice(String object1) { NumberFormat fmt = NumberFormat.getCurrencyInstance(); double theprice = 0; for (int i = 0; i < count; i++) { Book o1 = collection[i]; if (object1.equals(o1.getName())) theprice = o1.getPrice(); } return theprice; } public String toString() { NumberFormat fmt = NumberFormat.getCurrencyInstance(); String result = "Heres are book we have:"; for (int book = 0; book < count; book++) result += collection[book].toString() + "\n"; result += "We totally have" + count + "books."; return result; } }
package experiment5; import java.util.Scanner; public class Person { private int birthyear; private String name,sex,birthday; public int age; public String ID; public void putMessage() { Scanner scan=new Scanner(System.in); System.out.println("请输入您的姓名"); name=scan.nextLine(); System.out 4000 .println("请输入您的性别"); sex=scan.nextLine(); System.out.println("请输入您的生日:年:"); birthyear=scan.nextInt(); System.out.println("请输入您的生日:月日:"); int birthmd=scan.nextInt(); birthday=Integer.toString(birthyear)+Integer.toString(birthmd); } public int countAge() { age=2015-birthyear; return age; } public String getID() { Scanner scan=new Scanner(System.in); System.out.println("请输入身份证前六位"); String IDq6=scan.nextLine(); System.out.println("请输入身份证后四位"); String IDh4=scan.nextLine(); ID=IDq6+birthday+IDh4; return ID; } public void printMessage() { System.out.println("name:"+name+"\nsex:"+sex+"\nage:"+age+ "\nbirthday:"+birthday+"\nID:"+ID); } }
package experiment5; public class testPerson { public static void main(String []args) { Person o1; o1=new Person(); o1.putMessage(); o1.getID(); o1.countAge(); o1.printMessage(); }}
package experiment5;
import java.text.NumberFormat;
import java.util.Scanner;
public class together {
public static void main (String []args) {
Person o1;Bag bag=new Bag();
o1=new Person();
o1.putMessage();
o1.getID();
o1.countAge();
BookStore books=new BookStore();
books.addbook("My", 18.00);
books.addbook("Heart", 9.99);
books.addbook("Will", 32.50);
books.addbook("Go", 23.50);
books.addbook("On", 100.00);
System.out.println(books);
int i=0;
String choice = "y";
double total=0;
NumberFormat fmt=NumberFormat.getCurrencyInstance();
System.out.println("请选择合适的书包大小");
String[] Bag = bag.chooseBag();
while (choice.equalsIgnoreCase("y")) {
System.out.println("请输入你想买的书");
Scanner scan = new Scanner(System.in);
Bag[i]=scan.nextLine();
System.out.println(fmt.format(books.getPrice(Bag[i])));
total+=books.getPrice(Bag[i]);
i++;
System.out.println("还想继续买吗?Y是N否");
choice = scan.nextLine();}
if (choice.equalsIgnoreCase("n")) {
o1.printMessage();
System.out.println("您现在包里装了"+i+"本书,价格总计:"+fmt.format(total));
}
}
}
相关文章推荐
- 山东大学Java程序设计课程实验报告4条件和循环语句
- java程序设计课程实验报告1
- JAVA课程实验报告 实验二 JAVA面向对象程序设计
- JAVA课程实验报告 实验二 Java面向对象程序设计
- 《万年历》课程设计实验报告
- Java课程实验报告 实验一 Java开发环境的熟悉
- java程序设计第二次实验报告
- Java面向对象程序设计 java实验报告第三个
- [Java]数据结构课程设计:迷宫老鼠2.0(完整项目代码与设计报告)
- Java程序设计第四次实验报告
- 实验2 多线程应用程序设计与串行端口程序设计 实验报告
- 20135213 20135231 信息安全系统设计基础课程第二次实验报告
- 课程 Java Web程序设计15: 使用和部署Web应用
- Java课程实验报告 实验四 Java网络编程及安全
- JAVA课程实验报告 实验三 敏捷开发与XP实践
- java程序设计课程 大型实验 - 记事本
- 【8086】中国电力出版社 微机原理实验与课程设计指导书 实验二 数据处理实验程序设计 答案
- C程序设计课程第五次上机实验报告
- Java课程实验报告 实验三 敏捷开发与XP实践
- C程序设计课程-第七次实验报告