实验五 Java 语言特殊类和泛型应用练习 (课内实验)

本文详细介绍了在Java编程中应用特殊类(如内部类、集合类和字符串)、泛型以及设计学生信息管理系统的过程,涉及实验目的、要求、多个示例程序展示和总结,强调了基础知识和实践操作的重要性。
摘要由CSDN通过智能技术生成

一、实验目的

本次实验的主要目的是练习在程序中应用 Java 特殊类和泛型进行程序设计,
熟悉内部类、集合类、字符串和泛型的基础知识,掌握应用内部类、集合类、字
符串和泛型完成程序设计的基本方法。

二、实验要求

  1. 认真阅读实验内容,完成实验内容所设的题目。
  2.  能够应用多种编辑环境编写Java语言源程序。
  3.  认真体会Java语言特殊类和泛型的概念。
  4.  将实验结果书写在实验报告中。

三、实验内容

运行下列程序,观察程序的运行结果

A、程序一

public class Fyt {
    public static void main(String[] args) {
    Parcel p = new Parcel();
 p.testShip();
    Parcel.Contents c = p.new Contents(33);
    Parcel.Destination d = p.new Destination( "Hawii" );
 p.setProperty( c, d );
 p.ship();
}
}
class Parcel {
    private Contents c;
    private Destination d;
    class Contents {
        private int i;
        Contents( int i ){ this.i = i; }
        int value() { return i; }
    }
    class Destination {
        private String label;
        Destination(String whereTo) {label = whereTo;}
        String readLabel() { return label; }
    }
    void setProperty( Contents c, Destination d ){
        this.c =c; this.d = d;
    }
    void ship(){
        System.out.println( "move "+ c.value() +" to "+ d.readLabel() );
    }
    public void testShip() {
        c = new Contents(22);
        d = new Destination("Beijing");
        ship();
    }
}

B程序二

package org.example;

public class Main {
    public  static void main(String[] args) {
        {
            Circle circle = new Circle(10);
            circle.Showdraw();
        }
    }

    static class Circle {
        private double radius = 0;
        public static int count = 1;

        public Circle(double radius) {
            this.radius = radius;
        }

        public void Showdraw() {
            Draw draw = new Draw();
            draw.drawSahpe();
        }

        class Draw {
            public void drawSahpe() {
                System.out.println(radius);
                System.out.println(count);
            }
        }
    }


}

C程序三

package org.example;

public class Main {
    public  static void main(String[] args){
        Object obj = new Outer().makeTheInner(47);
        System.out.println("Hello World!" + obj.toString());
    }
}class Outer
{
    private int size = 5;
    public Object makeTheInner(int localVar)
    {
        final int finalLocalVar = 99;
        class Inner{
            public String toString(){
                return("InnerSize:" + size + "localVar:" + localVar + "finalLocalVar:" + finalLocalVar);
            }
        }
        return new Inner();
    }
}

D程序四

package org.example;

public class Main {
    public  static void main(String[] args){
        Outer outer=new Outer();
        Object o=outer.makeTheInner(20);
        System.out.println(o);
    }
}
class Outer
{ private int size = 5;
    public Object makeTheInner(int localVar)
    {
        final int finalLocalVar = 99;
        return new Object(){
            public String toString(){
                return ("InnerSize:" + size + "finalLocalVar:" +
                        finalLocalVar+" localVar:"+localVar);
            }
        };
    }
}

E程序五

package org.example;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class Main {
    public static void main(String[] args) {
        String aString = "A", bString = "B", cString = "C", dString = "D",
                eString = "E";
        List<String> list = new LinkedList<>(); // 创建集合
        list.add(aString);
        list.add(bString);
        list.add(eString);
// 输出语句,用迭代器
        Iterator<String> iter = list.iterator(); // 创建集合迭代器
        while(iter.hasNext()) { // 遍历集合中的元素
            System.out.print(iter.next() + " ");
        }
        System.out.println(); // 换行
        list.set(1, cString); // 将索引位置1的对象修改为对象bString
        Iterator<String> it = list.iterator();
        while(it.hasNext()) {
            System.out.print(it.next() + " ");
        }
    }
}

F程序六

package org.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
    public static void main(String[] args) {
        DAO<User> dao = new DAO<User>();
        dao.map = new HashMap<String,User>();
        dao.save("1001", new User(1, 32, "梁朝伟"));
        dao.save("1002", new User(2,34,"汤唯"));
        dao.save("1003", new User(3,23,"刘嘉玲"));
        User u = dao.get("1002");
        System.out.println(u);
        dao.update("1002", new User(4,45,"成龙"));
        dao.delete("1003");
        List<User> list = dao.list();
        System.out.println(list);
    }}
class DAO<T> {
    Map<String,T> map;
    public void delete(String id){
        map.remove(id);
    }
    public List<T> list(){
        List<T> list = new ArrayList<T>();
        for(String s : map.keySet()){
            list.add(map.get(s));
        }
        return list;
    }
    public void update(String id,T entity){
        map.put(id, entity);
    }
    public T get(String id){
        return map.get(id);
    }
    public void save(String id,T entity){
        map.put(id, entity);
    }
}
class User {
    private int id;private int age;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public User(int id, int age, String name) {
        super();
        this.id = id;
        this.age = age;
        this.name = name;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", age=" + age + ", name=" + name + "]";
    }@Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if (age != other.age)
            return false;
        if (id != other.id)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }}

四、编写程序完成下列功能

1.使用Map集合统计字符串中“123, 456, 789, 123, 456”中每个整数出现的次
数并打印出来。
package org.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        String input = "123, 456, 789, 123, 456";
        String[] numbers = input.split(",\\s*");
        HashMap<String, Integer> countMap = new HashMap<>();

        for (String number : numbers) {
            if (countMap.containsKey(number)) {
                countMap.put(number, countMap.get(number) + 1);
            } else {
                countMap.put(number, 1);
            }
        }

        for (String number : countMap.keySet()) {
            System.out.println(number + ": " + countMap.get(number));
        }
    }
}
2. 定义一个Father和Child类,并进行测试。
要求如下:
Father类为外部类,类中定义一个私有的String类型的属性name,name的值
为“zhangjun”。
Child类为Father类的内部类,其中定义一个introFather()方法,方法中调用
Father类的name属性。
定义一个测试类Test,在Test类的main()方法中,创建Child对象,并调用
introFather ()方法。
public class Main {
    private String name = "fyt";

    public String getName() {
        return name;
    }

    public class Child {
        public void introFather() {
            System.out.println("Father's name is " + Main.this.getName());
        }
    }
}

public class Test {
    public static void main(String[] args) {
        Main main = new Main();
        Main.Child child = main.new Child();
        child.introFather();
    }
}
3.声明一个泛型类:zhui(锥),表示锥型,并定义一个求锥型体积的方法,
编写程序分别输出圆锥和方锥的体积。
注:锥型体积等于底面积和高的乘积。
package org.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
public class Main<T extends Number> {
    private T bottomArea;
    private T height;

    public Main(T bottomArea, T height) {
        this.bottomArea = bottomArea;
        this.height = height;
    }

    public double getVolume() {
        return bottomArea.doubleValue() * height.doubleValue() / 3;
    }

    public static void main(String[] args) {
        // 圆锥底面积为半径r的圆的面积,高为h
        Main<Double> circularCone = new Main<>(Math.PI * Math.pow(2.0, 2.0), 5.0);
        System.out.println("The volume of circular cone is: " + circularCone.getVolume());

        // 方锥底面积为边长为a的正方形的面积,高为h
        Main<Integer> squareCone = new Main<>(4, 6);
        System.out.println("The volume of square cone is: " + squareCone.getVolume());
    }
}

五、实验总结

在本次实验中,我通过设计学生信息管理系统,深入学习了Java特殊类和泛型的应用。通过实践,我掌握了内部类、集合类、字符串和泛型的基础知识,并学会了如何巧妙地运用它们进行程序设计。这次实验让我更加熟练地使用集合类存储和操作数据,以及利用泛型实现灵活的操作方法。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亚力山大抵

谢谢你希望我的博客对你有所帮助

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值