您的位置:首页 > 编程语言 > Java开发

山东大学Java程序设计课程实验报告5设计和使用类

2017-11-24 23:41 519 查看
实验目的:

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

}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: