用java编写5个代码1.编写程序,在控制台中输出9*9乘法表。 2.定义一个用于存放班级分数的数组,长度为10,任意输入10个分数,求出班级的总分数 3. 设计一个Football类,拥有材质,直径,品牌,重量,生产日期,销售日期等属性;对各个属性的setter方法和getter方法。写出对应Java代码。 4. 设计一个抽象类Apple类,拥有重量,品种,单价,产地,采摘日期,销售日期等属性;一个抽象方法:计算运费。写出对应Java代码。 5. 分析以下需求,并用代码实现。描述笔记本类,实现笔记本使用USB鼠标。(1)定义USB接口:开启功能,关闭功能。(2)定义笔记本类:具备开机功能,关机功能,具备使用USB设备的功能。(3)定义鼠标类:要符合USB接口。(4) 定义测试类:创建电脑对象,依次调用开机方法,使用USB设备, 关机方法。 程序运行打印效果如下:开机 连接鼠标的USB 断开鼠标的USB 关机。
时间: 2024-03-25 08:39:56 浏览: 105
1. 输出9*9乘法表的Java代码:
```
public class MultiplicationTable {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + i * j + "\t");
}
System.out.println();
}
}
}
```
2. 计算班级总分数的Java代码:
```
import java.util.Scanner;
public class ClassScores {
public static void main(String[] args) {
int[] scores = new int[10];
int totalScore = 0;
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
System.out.print("请输入第" + (i + 1) + "个学生的分数:");
scores[i] = scanner.nextInt();
totalScore += scores[i];
}
System.out.println("班级总分数为:" + totalScore);
}
}
```
3. Football类的Java代码:
```
import java.util.Date;
public class Football {
private String material;
private int diameter;
private String brand;
private double weight;
private Date productionDate;
private Date saleDate;
public String getMaterial() {
return material;
}
public void setMaterial(String material) {
this.material = material;
}
public int getDiameter() {
return diameter;
}
public void setDiameter(int diameter) {
this.diameter = diameter;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public Date getProductionDate() {
return productionDate;
}
public void setProductionDate(Date productionDate) {
this.productionDate = productionDate;
}
public Date getSaleDate() {
return saleDate;
}
public void setSaleDate(Date saleDate) {
this.saleDate = saleDate;
}
}
```
4. Apple抽象类的Java代码:
```
import java.util.Date;
public abstract class Apple {
private double weight;
private String variety;
private double unitPrice;
private String origin;
private Date pickDate;
private Date saleDate;
public abstract double calculateFreight();
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String getVariety() {
return variety;
}
public void setVariety(String variety) {
this.variety = variety;
}
public double getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
public Date getPickDate() {
return pickDate;
}
public void setPickDate(Date pickDate) {
this.pickDate = pickDate;
}
public Date getSaleDate() {
return saleDate;
}
public void setSaleDate(Date saleDate) {
this.saleDate = saleDate;
}
}
```
5. 笔记本类与鼠标类的Java代码:
```
interface USB {
void turnOn();
void turnOff();
}
class Laptop implements USB {
public void turnOn() {
System.out.println("开机");
}
public void turnOff() {
System.out.println("关机");
}
public void connectUSB(USB usb) {
System.out.println("连接" + usb.getClass().getSimpleName() + "的USB");
}
public void disconnectUSB(USB usb) {
System.out.println("断开" + usb.getClass().getSimpleName() + "的USB");
}
}
class Mouse implements USB {
public void turnOn() {
System.out.println("鼠标开启功能");
}
public void turnOff() {
System.out.println("鼠标关闭功能");
}
}
public class Test {
public static void main(String[] args) {
Laptop laptop = new Laptop();
Mouse mouse = new Mouse();
laptop.turnOn();
laptop.connectUSB(mouse);
laptop.disconnectUSB(mouse);
laptop.turnOff();
}
}
```
阅读全文