上传了实验六,已确认无敏感信息
This commit is contained in:
+7
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"java.project.sourcePaths": ["src"],
|
||||||
|
"java.project.outputPath": "bin",
|
||||||
|
"java.project.referencedLibraries": [
|
||||||
|
"lib/**/*.jar"
|
||||||
|
]
|
||||||
|
}
|
||||||
Executable
+18
@@ -0,0 +1,18 @@
|
|||||||
|
## Getting Started
|
||||||
|
|
||||||
|
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
|
||||||
|
|
||||||
|
## Folder Structure
|
||||||
|
|
||||||
|
The workspace contains two folders by default, where:
|
||||||
|
|
||||||
|
- `src`: the folder to maintain sources
|
||||||
|
- `lib`: the folder to maintain dependencies
|
||||||
|
|
||||||
|
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
|
||||||
|
|
||||||
|
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
|
||||||
|
|
||||||
|
## Dependency Management
|
||||||
|
|
||||||
|
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).
|
||||||
Executable
+18
@@ -0,0 +1,18 @@
|
|||||||
|
package task1;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Task {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int num1 = sc.nextInt();
|
||||||
|
int num2 = sc.nextInt();
|
||||||
|
/********* Begin *********/
|
||||||
|
try {
|
||||||
|
System.out.println(num1 / num2);
|
||||||
|
} catch (java.lang.ArithmeticException e) {
|
||||||
|
System.out.println("除数不能为0");
|
||||||
|
}
|
||||||
|
/********* End *********/
|
||||||
|
}
|
||||||
|
}
|
||||||
Executable
+35
@@ -0,0 +1,35 @@
|
|||||||
|
package task2;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Task {
|
||||||
|
/********* Begin *********/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
String username = sc.next();
|
||||||
|
// 判断用户名
|
||||||
|
if (username.length() < 3) {
|
||||||
|
try {
|
||||||
|
throw new MyException("用户名小于3位Exception");
|
||||||
|
} catch (MyException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
System.out.println("用户名格式正确");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class MyException extends java.lang.Exception {
|
||||||
|
public MyException() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/********* End *********/
|
||||||
Executable
+258
@@ -0,0 +1,258 @@
|
|||||||
|
package task3;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Task3 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
String pppName = sc.next();
|
||||||
|
int pppAge = sc.nextInt();
|
||||||
|
String bpName = sc.next();
|
||||||
|
int bpAge = sc.nextInt();
|
||||||
|
String ppcName = sc.next();
|
||||||
|
int ppcAge = sc.nextInt();
|
||||||
|
String bcName = sc.next();
|
||||||
|
int bcAge = sc.nextInt();
|
||||||
|
// 测试运动员(乒乓球运动员和篮球运动员)
|
||||||
|
// 乒乓球运动员
|
||||||
|
// 通过带参构造函数实例化PingPangPlayer对象ppp
|
||||||
|
// 输出'name---age'
|
||||||
|
// 分别调用sleep()、eat()、study()、speak()方法
|
||||||
|
/********* begin *********/
|
||||||
|
PingPangPlayer ppp = new PingPangPlayer(pppName, pppAge);
|
||||||
|
System.out.println(ppp.getName() + "---" + ppp.getAge());
|
||||||
|
ppp.sleep();
|
||||||
|
ppp.eat();
|
||||||
|
ppp.study();
|
||||||
|
ppp.speak();
|
||||||
|
/********* end *********/
|
||||||
|
System.out.println("----------------");
|
||||||
|
// 篮球运动员
|
||||||
|
// 通过带参构造函数实例化BasketballPlayer对象bp
|
||||||
|
// 输出'name---age'
|
||||||
|
// 分别调用sleep()、eat()、study()方法
|
||||||
|
/********* begin *********/
|
||||||
|
BasketballPlayer bp = new BasketballPlayer(bpName, bpAge);
|
||||||
|
System.out.println(bp.getName() + "---" + bp.getAge());
|
||||||
|
bp.sleep();
|
||||||
|
bp.eat();
|
||||||
|
bp.study();
|
||||||
|
/********* end *********/
|
||||||
|
System.out.println("----------------");
|
||||||
|
// 测试教练(乒乓球教练和篮球教练)
|
||||||
|
// 乒乓球教练
|
||||||
|
// 通过带参构造函数实例化PingPangCoach对象ppc
|
||||||
|
// 输出'name---age'
|
||||||
|
// 分别调用sleep()、eat()、teach()、speak()方法
|
||||||
|
/********* begin *********/
|
||||||
|
PingPangCoach ppc = new PingPangCoach(ppcName, ppcAge);
|
||||||
|
System.out.println(ppc.getName() + "---" + ppc.getAge());
|
||||||
|
ppc.sleep();
|
||||||
|
ppc.eat();
|
||||||
|
ppc.teach();
|
||||||
|
ppc.speak();
|
||||||
|
/********* end *********/
|
||||||
|
System.out.println("----------------");
|
||||||
|
// 篮球教练
|
||||||
|
// 通过带参构造函数实例化BasketballCoach对象bc
|
||||||
|
// 输出'name---age'
|
||||||
|
// 分别调用sleep()、eat()、teach()方法
|
||||||
|
/********* begin *********/
|
||||||
|
BasketballCoach bc = new BasketballCoach(bcName, bcAge);
|
||||||
|
System.out.println(bc.getName() + "---" + bc.getAge());
|
||||||
|
bc.sleep();
|
||||||
|
bc.eat();
|
||||||
|
bc.teach();
|
||||||
|
/********* end *********/
|
||||||
|
System.out.println("----------------");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 说英语接口 声明抽象方法speak()
|
||||||
|
interface SpeakEnglish {
|
||||||
|
/********* begin *********/
|
||||||
|
public abstract void speak();
|
||||||
|
/********* end *********/
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定义人的抽象类Person 封装name和age
|
||||||
|
// 无参构造函数
|
||||||
|
// 有参构造函数初始化name和age
|
||||||
|
// 定义具体方法sleep() 输出'人都是要睡觉的'
|
||||||
|
// 抽象方法eat()(吃的不一样)
|
||||||
|
abstract class Person {
|
||||||
|
/********* begin *********/
|
||||||
|
private String name;
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Person() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Person(String name, int age) {
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sleep() {
|
||||||
|
System.out.println("人都是要睡觉的");
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void eat();
|
||||||
|
/********* end *********/
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定义运动员Player(抽象类)继承自Person类
|
||||||
|
// 无参构造函数
|
||||||
|
// 有参构造函数初始化name和age
|
||||||
|
// 运动员学习内容不一样,抽取为抽象 定义抽象方法study()
|
||||||
|
abstract class Player extends Person {
|
||||||
|
/********* begin *********/
|
||||||
|
public Player() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Player(String name, int age) {
|
||||||
|
super(name, age);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void study();
|
||||||
|
/********* end *********/
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定义教练Coach(抽象类)继承自Person类
|
||||||
|
// 无参构造函数
|
||||||
|
// 有参构造函数初始化name和age
|
||||||
|
// 教练教的不一样 定义抽象方法teach()
|
||||||
|
abstract class Coach extends Person {
|
||||||
|
/********* begin *********/
|
||||||
|
public Coach() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Coach(String name, int age) {
|
||||||
|
super(name, age);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void teach();
|
||||||
|
/********* end *********/
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定义乒乓球运动员具体类PingPangPlayer 继承自Player类并实现SpeakEnglish类(兵乓球运动员需要说英语)
|
||||||
|
// 无参构造函数
|
||||||
|
// 有参构造函数初始化name和age
|
||||||
|
// 实现自己的eat()方法 输出'乒乓球运动员吃大白菜,喝小米粥'
|
||||||
|
// 实现自己的study()方法 输出'乒乓球运动员学习如何发球和接球'
|
||||||
|
// 实现自己的speak()方法 输出'乒乓球运动员说英语'
|
||||||
|
class PingPangPlayer extends Player implements SpeakEnglish {
|
||||||
|
/********* begin *********/
|
||||||
|
public PingPangPlayer() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public PingPangPlayer(String name, int age) {
|
||||||
|
super(name, age);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void eat() {
|
||||||
|
System.out.println("乒乓球运动员吃大白菜,喝小米粥");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void study() {
|
||||||
|
System.out.println("乒乓球运动员学习如何发球和接球");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void speak() {
|
||||||
|
System.out.println("乒乓球运动员说英语");
|
||||||
|
}
|
||||||
|
/********* end *********/
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定义篮球运动员具体类BasketballPlayer 继承自Player类 不需要继承接口,因为他不需要说英语
|
||||||
|
// 无参构造函数
|
||||||
|
// 有参构造函数初始化name和age
|
||||||
|
// 实现自己的eat()方法 输出'篮球运动员吃牛肉,喝牛奶'
|
||||||
|
// 实现自己的study()方法 输出'篮球运动员学习如何运球和投篮'
|
||||||
|
class BasketballPlayer extends Player {
|
||||||
|
/********* begin *********/
|
||||||
|
public BasketballPlayer() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public BasketballPlayer(String name, int age) {
|
||||||
|
super(name, age);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void eat() {
|
||||||
|
System.out.println("篮球运动员吃牛肉,喝牛奶");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void study() {
|
||||||
|
System.out.println("篮球运动员学习如何运球和投篮");
|
||||||
|
}
|
||||||
|
/********* end *********/
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定义乒乓球教练具体类 PingPangCoach 继承自Coach类并实现SpeakEnglish类(兵乓球教练需要说英语)
|
||||||
|
// 无参构造函数
|
||||||
|
// 有参构造函数初始化name和age
|
||||||
|
// 实现自己的eat()方法 输出'乒乓球教练吃小白菜,喝大米粥'
|
||||||
|
// 实现自己的teach()方法 输出'乒乓球教练教如何发球和接球'
|
||||||
|
// 实现自己的speak()方法 输出'乒乓球教练说英语'
|
||||||
|
class PingPangCoach extends Coach implements SpeakEnglish {
|
||||||
|
/********* begin *********/
|
||||||
|
public PingPangCoach() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public PingPangCoach(String name, int age) {
|
||||||
|
super(name, age);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void eat() {
|
||||||
|
System.out.println("乒乓球教练吃小白菜,喝大米粥");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void teach() {
|
||||||
|
System.out.println("乒乓球教练教如何发球和接球");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void speak() {
|
||||||
|
System.out.println("乒乓球教练说英语");
|
||||||
|
}
|
||||||
|
/********* end *********/
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定义篮球教练具体类BasketballCoach 继承自Coach类 不需要继承接口,因为他不需要说英语
|
||||||
|
// 无参构造函数
|
||||||
|
// 有参构造函数初始化name和age
|
||||||
|
// 实现自己的eat()方法 输出'篮球教练吃羊肉,喝羊奶'
|
||||||
|
// 实现自己的teach()方法 输出'篮球教练教如何运球和投篮'
|
||||||
|
class BasketballCoach extends Coach {
|
||||||
|
/********* begin *********/
|
||||||
|
public BasketballCoach() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public BasketballCoach(String name, int age) {
|
||||||
|
super(name, age);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void eat() {
|
||||||
|
System.out.println("篮球教练吃羊肉,喝羊奶");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void teach() {
|
||||||
|
System.out.println("篮球教练教如何运球和投篮");
|
||||||
|
}
|
||||||
|
/********* end *********/
|
||||||
|
}
|
||||||
Executable
+15
@@ -0,0 +1,15 @@
|
|||||||
|
//Cuboid.java
|
||||||
|
package task4;
|
||||||
|
|
||||||
|
public class Cuboid extends Rectangle {
|
||||||
|
double height;
|
||||||
|
|
||||||
|
public Cuboid(double length, double width, double height) {
|
||||||
|
super(length, width);
|
||||||
|
this.height = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double volume() {
|
||||||
|
return this.length * this.width * this.height;
|
||||||
|
}
|
||||||
|
}
|
||||||
Executable
+24
@@ -0,0 +1,24 @@
|
|||||||
|
//Rectangle.java
|
||||||
|
package task4;
|
||||||
|
|
||||||
|
public class Rectangle {
|
||||||
|
double length;
|
||||||
|
double width;
|
||||||
|
|
||||||
|
public double getPerimeter() {
|
||||||
|
return (this.length + this.width) * 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getArea() {
|
||||||
|
return this.length * this.width;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Rectangle(double length, double width) {
|
||||||
|
this.length = length;
|
||||||
|
this.width = width;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Rectangle() {
|
||||||
|
this(1.0, 1.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
Executable
+20
@@ -0,0 +1,20 @@
|
|||||||
|
//Test.java
|
||||||
|
package task4;
|
||||||
|
|
||||||
|
public class Test {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Rectangle r1 = new Rectangle(3, 4);
|
||||||
|
System.out.println("r1's perimeter:\t" + r1.getPerimeter());
|
||||||
|
System.out.println("r1's area:\t" + r1.getArea());
|
||||||
|
System.out.println("__________________________________");
|
||||||
|
Rectangle r2 = new Rectangle();
|
||||||
|
System.out.println("r2's perimeter:\t" + r2.getPerimeter());
|
||||||
|
System.out.println("r2's area:\t" + r2.getArea());
|
||||||
|
System.out.println("__________________________________");
|
||||||
|
Cuboid c = new Cuboid(3, 4, 5);
|
||||||
|
System.out.println("bottom of c's perimeter:\t" + c.getPerimeter());
|
||||||
|
System.out.println("bottom c's area:\t" + c.getArea());
|
||||||
|
System.out.println("c's volume:\t" + c.volume());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user