概述
实验目的与要求:
实验目的:掌握常用的集合类,能够较为熟练的查阅Java提供的常见的类,并进行程序设计。
实验要求:
(1). 编写Java应用程序,计算菜单“北京烤鸭:189元;西芹炒肉:12.9元;酸菜鱼:69元;铁板牛柳:32元”的总价格。在报告中附上程序截图、完整的运行结果和简要文字说明。(10分)
(2). 编写Java应用程序,根据用户输入的两个日期,计算两个日期之间间隔的天数。注:用户输入的日期格式为“××××年××月××日”。在报告中附上程序截图、完整的运行结果和简要文字说明。(10分)
(3). 一个科研问题中需要实现两个浮点矩阵的乘法,这两个矩阵的行、列规模非常大,同时这个矩阵中有超过99%的元素为0,其他元素为浮点数。请编写一个Java应用程序,实现具有上述特点的矩阵乘法,要求使用HashMap。在报告中附上程序截图、完整的运行结果和简要文字说明。(30分)
(4). 某车站有检查危险品的机器(machine),如果发现危险品会发出警告。编程Java应用程序来模拟该机器发现危险品时发出警告的情景。具体:(i) 编写一个Exception的子类DangerException,该子类可以创建异常对象,该异常对象调用toShow()方法输出“属于危险品”;(ii) 编写一个Machine类,该类的方法checkBag(Goods goods)如果发现参数goods是危险品(goods的isDanger属性是true)就抛出DangerException异常;(iii) 程序在主类的main()方法中的try-catch语句的try部分让Machine类的实例调用checkBag(Goods goods)方法,如果发现危险品就在try-catch语句的catch部分处理危险品。在报告中附上程序截图、完整的运行结果和简要文字说明。(50分)
(1). 编写Java应用程序,计算菜单“北京烤鸭:189元;西芹炒肉:12.9元;酸菜鱼:69元;铁板牛柳:32元”的总价格。在报告中附上程序截图、完整的运行结果和简要文字说明。(10分)
设计:采用双列集合TreeMap以键值对的方式进行存储
package Hw7;
import java.util.*;
public class Menu {
private TreeMap<String,Double> treeMap;
Menu(){
treeMap = new TreeMap<>();
}
public void setTreeMap(){
treeMap.put("北京烤鸭", 189.0);
treeMap.put("西芹炒肉", 12.9);
treeMap.put("酸菜鱼", 69.0);
treeMap.put("铁板牛柳", 32.0);
}
public double getSum(){
double ret = 0;
Collection<Double> values = treeMap.values();
for (Double value : values) {
ret+=value;
}
return ret;
}
public static void main(String[] args) {
Menu menu = new Menu();
menu.setTreeMap();
System.out.println("价格的总和是:");
System.out.println(menu.getSum());
}
}
(2). 编写Java应用程序,根据用户输入的两个日期,计算两个日期之间间隔的天数。注:用户输入的日期格式为“××××年××月××日”。在报告中附上程序截图、完整的运行结果和简要文字说明。(10分)
设计:使用Date类,注意日期要格式化,还要实现用户输入
package Hw7;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class MyDate {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
Scanner sc = new Scanner(System.in);
Date date1 = sdf.parse(sc.nextLine());
Date date2 = sdf.parse(sc.nextLine());
long d1 = date1.getTime();
long d2 = date2.getTime();
long res = (d1-d2)/(24*60*60*1000);
System.out.println("相差天数为:");
System.out.println(res);
}
}
(3). 一个科研问题中需要实现两个浮点矩阵的乘法,这两个矩阵的行、列规模非常大,同时这个矩阵中有超过99%的元素为0,其他元素为浮点数。请编写一个Java应用程序,实现具有上述特点的矩阵乘法,要求使用HashMap。在报告中附上程序截图、完整的运行结果和简要文字说明。(30分)
设计:由于矩阵较为稀疏,所以采用键值对的方式存储非0数值,其他位置默认为0,重写Position类的equals方法实现比较
package Hw7;
import java.util.*;
class Position {
public int x;
public int y;
public Position() {
x = 0;
y = 0;
}
public Position(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Position position = (Position) o;
return x == position.x && y == position.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
public class ThinMatrix {
public int m; //行规模
public int n; //列规模
public HashMap<Position, Double> hashMap;
public ThinMatrix(int m, int n) {
this.m = m;
this.n = n;
hashMap = new HashMap<>() {
@Override
public String toString() {
String temp = "";
for (Position position : this.keySet()) {
temp += " {(" + position.x + "," + position.y + ")=>" + this.get(position) + "} ";
}
return temp;
}
};
}
public void setHashMap() {
Scanner sc = new Scanner(System.in);
int times = sc.nextInt();
for (int i = 0; i < times; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
Double value = sc.nextDouble();
hashMap.put(new Position(x, y), value);
}
}
public ThinMatrix multiplyMatrix(ThinMatrix thinMatrix) {
ThinMatrix res = new ThinMatrix(this.m, thinMatrix.n);
for (int i = 0; i < this.m; i++) {
for (int j = 0; j < thinMatrix.n; j++) {
double sum = 0.0;
for (int k = 0; k < this.n; k++) {
if (this.hashMap.get(new Position(i, k)) != null && thinMatrix.hashMap.get(new Position(k, j)) != null) {
sum += this.hashMap.get(new Position(i, k)) * thinMatrix.hashMap.get(new Position(k, j));
}
}
if (sum != 0) {
res.hashMap.put(new Position(i, j), sum);
}
}
}
return res;
}
@Override
public String toString() {
return "ThinMatrix{" +
"m=" + m +
", n=" + n +
", hashMap=" + hashMap +
'}';
}
public static void main(String[] args) {
ThinMatrix thinMatrix1 = new ThinMatrix(1, 2);
ThinMatrix thinMatrix2 = new ThinMatrix(2, 3);
thinMatrix1.setHashMap();
thinMatrix2.setHashMap();
System.out.println("结果是:");
System.out.println(thinMatrix1.multiplyMatrix(thinMatrix2));
}
}
(4). 某车站有检查危险品的机器(machine),如果发现危险品会发出警告。编程Java应用程序来模拟该机器发现危险品时发出警告的情景。具体:(i) 编写一个Exception的子类DangerException,该子类可以创建异常对象,该异常对象调用toShow()方法输出“属于危险品”;(ii) 编写一个Machine类,该类的方法checkBag(Goods goods)如果发现参数goods是危险品(goods的isDanger属性是true)就抛出DangerException异常;(iii) 程序在主类的main()方法中的try-catch语句的try部分让Machine类的实例调用checkBag(Goods goods)方法,如果发现危险品就在try-catch语句的catch部分处理危险品。在报告中附上程序截图、完整的运行结果和简要文字说明。(50分)
设计:实现Goods,Machine,DangerException类
package Hw7;
public class Machine {
public void checkBag(Goods goods) throws DangerException {
for (Boolean aBoolean : goods.isDanger) {
if(aBoolean){
throw new DangerException();
}
}
}
public static void main(String[] args) {
String[] name = {"纸巾","打火机","铅笔"};
Boolean[] isDanger = {false,true,false};
Goods goods = new Goods(name,isDanger);
Machine machine = new Machine();
try{
machine.checkBag(goods);
}catch (DangerException e){
e.toShow();
}
}
}
class DangerException extends Exception{
public void toShow(){
System.out.println("属于危险品");
}
}
class Goods{
public String[] name;
public Boolean[] isDanger;
public Goods(String[] name, Boolean[] isDanger) {
this.name = name;
this.isDanger = isDanger;
}
}
最后
以上就是机智戒指为你收集整理的Java语言程序设计 选实验4 常用集合类使用的全部内容,希望文章能够帮你解决Java语言程序设计 选实验4 常用集合类使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复