帮我用Java写一个1、有四个类,基类Store,直接或间接派生出Mobile、Mp3Player、Product这几个类,其中Mobile、Mp3Player是Product的子类,Product类实现Comparable接口,重写了Comparable接口中方法compareTo,实现了product对象按照价格排序。基本要求:(1)写出Product、Mobile、Mp3Player类的源码,是否有构造函数根据输出结果自行决定。(2)在主类Store中实例化多个Mobile类与Mp3Player的实例,分别将这些实例用ArrayList与LinkedList存放,最后用StringBuider存放并将其输出。(3)用迭代器(iterator)将实例对象输出(要求用加强型for循环)。 2、已知某学校的教学课程内容安排如下 : 完成下列要求:(1)使用一个Map,以老师的名字作为键,以老师教授的课程名作为值,表示上述课程安排。(2)增加了一位新老师Allen 教JDBC。(3)Lucy 改为教CoreJava。(4)用两种方式遍历Map,输出所有的老师及老师教授的课程。(5)利用Map,输出所有教JSP 的老师。
时间: 2023-06-27 22:01:28 浏览: 271
1. 代码如下:
Product类:
```java
public class Product implements Comparable<Product> {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
@Override
public int compareTo(Product o) {
if (this.price > o.getPrice()) {
return 1;
} else if (this.price < o.getPrice()) {
return -1;
} else {
return 0;
}
}
}
```
Mobile类:
```java
public class Mobile extends Product {
private String brand;
public Mobile(String name, double price, String brand) {
super(name, price);
this.brand = brand;
}
public String getBrand() {
return brand;
}
}
```
Mp3Player类:
```java
public class Mp3Player extends Product {
private int storage;
public Mp3Player(String name, double price, int storage) {
super(name, price);
this.storage = storage;
}
public int getStorage() {
return storage;
}
}
```
Store类:
```java
import java.util.*;
public class Store {
public static void main(String[] args) {
ArrayList<Mobile> mobileList = new ArrayList<>();
mobileList.add(new Mobile("iPhone", 999.99, "Apple"));
mobileList.add(new Mobile("Galaxy S21", 899.99, "Samsung"));
LinkedList<Mp3Player> mp3PlayerList = new LinkedList<>();
mp3PlayerList.add(new Mp3Player("iPod Touch", 199.99, 64));
mp3PlayerList.add(new Mp3Player("Walkman", 149.99, 32));
StringBuilder sb = new StringBuilder();
for (Mobile mobile : mobileList) {
sb.append(mobile.getName()).append(", ").append(mobile.getBrand()).append(", $").append(mobile.getPrice()).append("\n");
}
for (Mp3Player mp3Player : mp3PlayerList) {
sb.append(mp3Player.getName()).append(", ").append(mp3Player.getStorage()).append("GB, $").append(mp3Player.getPrice()).append("\n");
}
System.out.println(sb.toString());
Iterator<Mobile> mobileIterator = mobileList.iterator();
while (mobileIterator.hasNext()) {
Mobile mobile = mobileIterator.next();
System.out.println(mobile.getName() + ", " + mobile.getBrand() + ", $" + mobile.getPrice());
}
for (Mp3Player mp3Player : mp3PlayerList) {
System.out.println(mp3Player.getName() + ", " + mp3Player.getStorage() + "GB, $" + mp3Player.getPrice());
}
}
}
```
2. 代码如下:
```java
import java.util.*;
public class School {
public static void main(String[] args) {
Map<String, String> courseMap = new HashMap<>();
courseMap.put("Tom", "Java");
courseMap.put("Jerry", "Python");
courseMap.put("Lucy", "JavaScript");
courseMap.put("Allen", "JDBC");
courseMap.put("Lucy", "CoreJava");
for (Map.Entry<String, String> entry : courseMap.entrySet()) {
System.out.println(entry.getKey() + " teaches " + entry.getValue());
}
System.out.println();
Set<Map.Entry<String, String>> entrySet = courseMap.entrySet();
Iterator<Map.Entry<String, String>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
if (entry.getValue().equals("JSP")) {
System.out.println(entry.getKey());
}
}
}
}
```
阅读全文