1 .实 验 名 称 类与对象(1)
2 .实 验 目 的 (1)学会类的实现、类的使用和用 UML 类图描述类之间的关系,能够对实际的案例进行分 析和建模,并画出类图。 (2)能够根据类图进行编码实现。 (3)理解构造方法的概念和重载的概念,能在程序设计中灵活应用。 (4)巩固数组在面向对象程序设计中的运用;
3 .训 练 知 识 点 集 群 (1)类的语法(类的成员变量和成员方法、构造方法、成员变量的 getter、setter 方法等) (2)类图(单个类的类图和类之间的关系) (3)使用数组构造线性表
4 .实 验 内 容 (1)计算某长方体或正方体盒子的体积 (2)课程融合之线性表 ➢ 定义一个类 MyArrayList,用数组模拟实现数据结构中线性表的基本功能,该类中最少包 含的属性有:data(String[])、size(int)、CAPACITY(int); ➢ 该类中方法最少包括线性表的初始化、插入 、查找、遍历线性表、删除、返回线性表 长度、线性表判空、清空线性表、替换、获取元素、判断线性表中是否包含某个元素、 反向查找、末尾插入元素; ➢ 定义一个主类,在主方法中创建线性表类对象并调用上述方法、完成功能演示。
一 计算机类Calculator:
package Calculator;
import java.util.Scanner;
class Calculator {
private float num1;
private float num2;
private String op;
public Calculator(float num1,float num2,String op){
this.num1=num1;
this.num2=num2;
this.op=op;
}
public void cal(String op){
if(op.equals("+")){
float i=num1+num2;
System.out.println(i);
}
else if(op.equals("-")){
float i=num1-num2;
System.out.println(i);
}
else if(op.equals("*")){
float i=num1*num2;
System.out.println(i);
}
else if(op.equals("/")){
float i=num1/num2;
System.out.println(i);
}
}
}
public class TestCalculator{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入操作符");
String op=sc.next();
System.out.println("请输入num1");
float num1=sc.nextFloat();
System.out.println("请输入num2");
float num2=sc.nextFloat();
Calculator c=new Calculator(num1,num2,op);
c.cal(op);
}
}
二 电商购物平台:
import java.util.Scanner;
class category {
private int id;//编号
private String fristlevel;//一级目录
private String secondlevel;//二级目录
public category(int id,String fristlevel,String secondlevel){
this.id=id;
this.fristlevel = fristlevel;
this.secondlevel = secondlevel;
}
public String toString(){
//小说>名著
return fristlevel+">"+secondlevel;
}
}
class book {
private int bid;//编号
private String name;//书名
private String author;//作者
private float price;//价格
private int num;//库存
private category category;//书籍类目
public book(int bid, String name, String auther, float price, int num, category category){
this.bid=bid;
this.name=name;
this.author=auther;
this.price=price;
this.num=num;
this.category=category;
}
public String getName() {
return name;
}
public int getBid() {
return bid;
}
public float getPrice() {
return price;
}
public String toString() {
return "book{" +
"bid=" + bid +
", name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", num=" + num +
", category=" + category +
'}';
}
}
class bookDataSet {
//数组的声明
public book[] books;//用数组模拟书籍数据库
//构造方法
public bookDataSet(){
books=new book[4];//测试
category category1=new category(1,"小说","名著");
category category2=new category(2,"计算机","算法");
books[0]=new book(1,"数据结构与算法","严蔚敏",54.6f,30,category2 );
books[1]=new book(2,"算法设计与分析","李华玲",78.6f,20,category2 );
books[2]=new book(3,"西游记","吴承恩",64.6f,20,category1 );
books[3]=new book(4,"红楼梦","曹雪芹",84.6f,20,category1 );
}
public book[] getBooks(){
return books;
}
public book SearchByName(String name){
book b=null;
for (int i=0;i<books.length;i++){
if(books[i].getName().equals(name)){
return books[i];
}
}
return b;
}
public book ChangeByBid(int bid) {
book c = null;
for (int j = 0; j < books.length; j++) {
if (books[j].getBid() == bid) {
return books[j];
}
}
return c;
}
}
public class testshopping {
public static void main(String[] args){
bookDataSet bookDataSet = new bookDataSet();
book[] books = bookDataSet.getBooks();
for(book b:books){
System.out.println(b);
}
System.out.println("请输入你想查找的书籍名:");
Scanner sc = new Scanner(System.in);
String name = sc.next();
book b = bookDataSet.SearchByName(name);
if(b!=null){
System.out.println("这本书的价格是:"+bookDataSet.SearchByName(name).getPrice());
}else System.out.println("查无此书");
//商品的录入
System.out.println("请输入你想录入书籍的数量:");
int num = sc.nextInt();
for (int j=0;j<num;j++) {
book[] books1 = new book[books.length + num+1];
for (int i = 0; i < books.length; i++) {
//将旧的数组赋值给新数组
books1[i] = books[i];
}
System.out.println("录入的书籍为:" + "5" + "水浒传" + "施耐庵" + 55.5f + 20 + new category(1,"小说","名著"));
books1[books.length + num-1] =
new book(5, "水浒传", "施耐庵", 55.5f, 20, new category(1,"小说","名著"));
for (book f : books1) {
System.out.println(f);
}
}
//商品的修改
System.out.println("请输入想要修改的书籍bid:");
bookDataSet bookDataSet2 = new bookDataSet();
book[] books2 = bookDataSet2.getBooks();
int id1 = sc.nextInt();
book c = bookDataSet2.ChangeByBid(id1);
if(c!=null){
System.out.println("要将其修改为:");
int bid2 = sc.nextInt();
books2 = new book[books.length+1];
books2[bid2] = books[bid2];
System.out.println("已修改为:"+"5" + "水浒传" + "施耐庵" + 55.5f + 20 + new category(1,"小说","名著"));
books2[b.getBid()] = new book(5 , "水浒传" , "施耐庵" ,55.5f , 20 , new category(1,"小说","名著"));
for (book f : books2) {
System.out.println(f);
}
}else System.out.println("查无此书");
}
}