求圆柱体的体积

1.要干什么?

Circle类定义圆的半径计算圆面积

Pillar类定义圆柱体的高计算体积,运用类的组合定义bottom

类的组合:类只要是public修饰的它就可以实现组合,在一个类当中去定义另一个类类型的变量

TestPillarVolume类测试

2.代码实现
public class Circle {
    private float r;
    public Circle(float r){
        this.r=r;
    }
        public float getArea(){
        float s=3.14f*r*r;
        return s;
    }
}
public class Pillar {
    Circle bottom;
    float height;
    public Pillar(Circle bottom,float height){
        this.bottom=bottom;
        this.height=height;
        }
   
    public float getVolume(){
        return bottom.getArea()*height;
    }
}
public class TestPillarVolume {
    public static void main(String[] args) {
        Circle a=new Circle(2.0f);
       Pillar pillar = new Pillar(a,5.0f);
        float v=pillar.getVolume();
        System.out.println("圆柱体的体积:"+v);
    }
}
3.注意事项
  • 创建对象和给属性赋值合二为一:Circle a = new Circle(2.0f);

  • 构造方法:和类名相同,无返回值;成员方法:自定义名字,有返回值。

课程融合之线性表用数组实现
1.要干什么?

完成代码补缺就OK

2.代码实现
//将一个元素添加到表尾
 public void add(String e) {
        ensureCapacity(); 
     /*作用:新建一个更长的数组,将原来数组的元素复制到新数组中,
     让data指向新数组*/
        data[size]=e;
        size++;
    }
3.注意事项
字符串数组长度固定,定义之后不可以改变长度。
两种解决办法:1.手动扩容 2.用集合
//将一个新元素e插入到线性表中索引号为index的位置
public void add(int index, String e) {
        ensureCapacity();
        for(int i=size;i>index;i--){
            data[i]=data[i-1];
        }
        data[index]=e;
        size++;
    }

用i指向最后一个元素,依次把元素放到后一位上去,直到向前遍历到index。

/**清空线性表(回到初始的缺省状态) */
    public void clear() {
        size=0;
        data=new String[16];
    }
/** contains(String e) 如果线性表中包含元素e,则返回true;否则返回false */
    public boolean contains(String e){
        for(int i=0;i<size;i++){
            if(data[i].equals(e){return true;}
        }
        return false;
    }
/** get(int index) 从线性表中获取索引号为index的元素 */
    public String get(int index){
        return data[index];
    }
/** indexOf(String e) 从前往后查找指定元素e,若找到,则返回该元素的索引号;
     * 若有多个元素匹配,则返回最先找到的那个元素;
     * 若找不到,则返回-1 */
    public int indexOf(String e){
        for(int i=0;i<size;i++){
            if(data[i].equals(e){return i;}
        }
        return -1;
    }
/** isEmpty() 如果线性表为空(没有任何元素),则返回true, 否则返回false*/
    public boolean isEmpty(){
        return size==0;
    }
/** lastIndexOf(String e) 从后往前反向查找指定元素e,若找到,则返回该元素的索引号;
     * 若有多个元素匹配,则返回最先找到的那个元素;
     * 若找不到,则返回-1 *  方法名:indexOf */
    public int lastIndexOf(String e){
        for(int i=size-1;i>=0;i--){
            if(data[i].equals(e)){return i;}
        }
        return -1;
    }
size-1
/** remove(String e) 从线性表中删除指定元素e,若有多个元素匹配,则删除首次匹配的元素,
     * 并将后面的所有元素向左移动一个位置,
     * 如果删除成功,则返回true;否则返回false。*/
    public boolean remove(String e){
        int indexOf(e);
        if(index==-1){return false;}
        for(int i=index;i<size;i++){
            data[i]=data[i+1];
        }
        size--;
        return true;
    }
/** remove(int index) 从线性表中删除指定位置index处的元素,
     * 并将后面的所有元素向左移动一个位置,
     * 如果删除成功,返回被删除的元素*/
    public String remove(int index){
        String a=data[index];
        for(int i=index;i<size;i++){
            data[i]=data[i+1];
        }
        size--;
        return a;
    }
/** set(int index, String e) 使用指定的元素e替换线性表中指定位置index处的元素,
     * 并返回被替换的那个旧元素 */
    public String set(int index,String e){
        String a=data[index];
        data[index]=e;
        return a;
    }
/** printAll() 遍历输出线性表中的所有元素 */
    public void printAll(){
        for(int i=0;i<size;i++){
            System.out.print(data[i]+" ");
        }
    }
电商购物平台
1.干什么?

根据题目要求完成就行

public class BookDataSet {
    private Book[] books;
    public BookDataSet(){
        Category c1 = new Category(1, "工具类", "软件编程");
        Category c2 = new Category(2, "小说类", "历史");
        books = new Book[4];
        books[0] = new Book("b001", "Java程序设计", "郎彭", 100, c1);
        books[1] = new Book("b002", "三国演文", "罗贯中", 50, c2);
        books[2] = new Book("b003", "水浒传", "施耐庵", 40, c2);
        books[3] = new Book("b004", "红楼梦", "曹雪芹", 30, c2);
    }
​
    public Book[] getBooks() {
        return books;
    }
​
    public Book queryById(String id) {
        for (Book book : books) {
            if (book.getBid().equals(id)) {
                return book;
            }
        }
        return null;
    }
    public void resetBook(String id, Book book) {
        for (int i = 0; i < books.length; i++) {
            if (books[i].getBid().equals(id)) {
                books[i] = book;
                break;
            }
​
        }
​
    }
​
}
public class Book {
​
    private String bid;
    public String name;
    public String author;
    public int number;
    public Category category;
​
    public Book(String bid, String name, String author, int number, Category category) {
        this.bid = bid;
        this.name = name;
        this.author = author;
        this.number = number;
        this.category = category
    }
​
    public void setBid(String bid) {
        this.bid = bid;
    }
​
    public String getBid() {
        return bid;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    public String getName() {
        return name;
    }
​
    public void setAuthor(String author) {
        this.author = author;
    }
​
    public String getAuthor() {
        return author;
    }
​
    public void setNumber(int number) {
        this.number = number;
    }
​
    public int getNumber() {
        return number;
    }
​
    public void setCategory(Category category) {
        this.category = category;
    }
​
    public Category getCategory() {
​
        return category;
​
    }
​
    @Override
​
    public String toString() {
​
        return "商品编号:" + bid + ",商品名称: " + name + ",商品类目:" + category + ",作者:" + author + ",库存数量" + number + "";
​
    }
​
}
public class Category {
    private int id;
    public String firstLevel;
    public String secondLevel;
    
    public Category(int id, String firstLevel, String secondLevel) {
        this.id = id;
        this.firstLevel = firstLevel;
        this.secondLevel = secondLevel;
    }
​
    public void setId(int id) {
        this.id = id;
    }
​
    public int getId() {
        return id;
    }
​
    public void setFirstLevel(String firstLevel) {
        this.firstLevel = firstLevel;
    }
​
    public String getFirstLevel() {
        return firstLevel;
    }
​
    public void setSecondLevel(String secondLevel) {
        this.secondLevel = secondLevel;
    }
​
    public String getSecondLevel() {
        return secondLevel;
    }
​
    public String toString() {
        return firstLevel + "->" + secondLevel;
    }
}
​
public class TestShopping {
    public static void main(String[] args) {
        BookDataSet dataset = new BookDataSet();
        Book book1 = dataset.queryById("b001");
        System.out.println(book1);
        Book book2 = dataset.queryById("b003");
        System.out.println(book2);
        Category newCat = new Category(1, "工具类", "Java编程");
        Book newBook = new Book("b001", "Java高级编程", "张三", 200, newCat);
        dataset.resetBook("b001", newBook);
        Book updatedBook = dataset.queryById("b001");
        System.out.println(updatedBook);
    }
}
​
​
选择题
  • 构造方法在创建对象时候就被调用

  • 类是对象的抽象

  • 方法中声明的局部变量不可以用访问修饰符:public,proteced,private修饰。

    原因:局部变量的作用域仅限于声明它的方法(或代码块)内部,其可见性和生命周期完全由所在的方法 / 代码块控制,不需要通过访问修饰符来限制访问范围。

  • 局部变量没有默认值

  • 类中的实例方法通过对象调用。

    什么是实例方法:成员方法中不被static修饰的

  • 在写属性的位置不能写执行语句,可以直接赋值

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐