《Java程序设计》实验5
1、定义一个表示圆的Circle类,其中有表示半径的double型的属性radius,计算圆周长的perimeter方法和计算面积的area方法。在Circle类的基础上,定义圆柱体Cylinder类和球体Sphere类,它们分别具备计算表面积的area方法和体积的volume方法,编写测试类进行测试。
package homework.test;
public class Circle {
double radius;
public Circle(double radius) {
this.radius = radius;
}
public double perimeter() {
return 2 * 3.14 * radius;
}
public double area() {
return 3.14 * radius * radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
package homework.test;
public class Cylinder extends Circle {
double height;
public Cylinder(double radius,double height) {
super(radius);
this.height = height;
}
void areaCy() {
double a = 2 * (3.14 * radius * radius) + (2 * 3.14 * radius) * height;
System.out.println("圆柱体表面积为:"+a);
}
void volumeCy() {
double v = super.area() * height;
System.out.println("圆柱体体积为:"+v);
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
package homework.test;
public class Sphere extends Circle {
public Sphere(double radius) {
super(radius);
}
void areaS() {
double a = 4 * 3.14 * radius * radius;
System.out.println("该球体表面积为:"+a);
}
void volumeS() {
double v = (4.0 / 3) * 3.14 * radius * radius * radius;
System.out.println("该球体体积为:"+v);
}
}
package homework.test;
public class CylinderSphereTest {
public static void main(String[] args) {
Cylinder cy = new Cylinder(2,3);
cy.areaCy();
cy.volumeCy();
Sphere s = new Sphere(3);
s.areaS();
s.volumeS();
}
}
2、编写程序,模拟某网站在线人数场景。 比如,张三登录系统后,输出:张三上线了,目前在线人数为4,李四下线了,目前在线人数为3。
package homework.test;
public class WebSystem {
String name;
int number;
public void number1() {
number = 3;
number++;
System.out.println("目前在线人数为"+number+"人");
}
public void number2() {
number--;
System.out.println("目前在线人数为"+number+"人");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
package homework.test;
import java.util.Scanner;
public class WebSystemTest {
public static void main(String[] args) {
WebSystem ws = new WebSystem();
while (true) {
System.out.println("登陆系统");
System.out.println("请输入您的名称:");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
ws.number1();
System.out.println("1.继续在线\t2.下线");
System.out.println("请选择您需要的操作对应的数字1-2:");
int choice = sc.nextInt();
switch(choice) {
case 1:
break;
case 2:
ws.number2();
break;
}
}
}
}
3、编写程序,模拟城市天气预报。从控制台输入若干个城市的天气数据,并在控制台打印输出。比如:城市:北京,天气状况:晴,最高温度:20摄氏度,最低温度:8摄氏度,风向:西北风,风力:3级。
package homework.test;
public class WeatherPrediction {
private String city;
private String condition;
private String MAX_temperature;
private String MIN_temperature;
private String wind_direction;
private String wind_power;
public WeatherPrediction(String city, String condition, String mAX_temperature, String mIN_temperature,
String wind_direction, String wind_power) {
super();
this.city = city;
this.condition = condition;
this.MAX_temperature = mAX_temperature;
this.MIN_temperature = mIN_temperature;
this.wind_direction = wind_direction;
this.wind_power = wind_power;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
public String getMAX_temperature() {
return MAX_temperature;
}
public void setMAX_temperature(String mAX_temperature) {
MAX_temperature = mAX_temperature;
}
public String getMIN_temperature() {
return MIN_temperature;
}
public void setMIN_temperature(String mIN_temperature) {
MIN_temperature = mIN_temperature;
}
public String getWind_direction() {
return wind_direction;
}
public void setWind_direction(String wind_direction) {
this.wind_direction = wind_direction;
}
public String getWind_power() {
return wind_power;
}
public void setWind_power(String wind_power) {
this.wind_power = wind_power;
}
void show() {
System.out.println("城市:"+city);
System.out.println("天气情况:"+condition);
System.out.println("最高温度:"+MAX_temperature);
System.out.println("最低温度:"+MIN_temperature);
System.out.println("风向:"+wind_direction);
System.out.println("风力:"+wind_power);
}
}
package homework.test;
public class WeatherPredictionTest {
public static void main(String[] args) {
WeatherPrediction w = new WeatherPrediction("北京","晴","20摄氏度","8摄氏度","西北风","3级");
w.show();
}
}
4、编写一个银行账户类,类的构成包括: (1)数据成员:用户的账号名称、用户的账户余额;
(2)方法包括:开户(设置账户名称及余额),利用构造方法完成; (3)查询余额。
package homework.test;
public class Account {
private String name;
private double money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void create(String name) {
this.name = name;
System.out.println("您创建的账号名称是:"+name);
}
public void setMoney(double money) {
this.money = money;
}
public void deposit(int a) {
this.money += a;
System.out.println("您存入了"+a+"元。");
}
public void withdraw(int b) {
this.money -= b;
System.out.println("您取出了"+b+"元。");
}
public void getBalance() {
System.out.println("当前您的余额为:"+money+"元。");
}
public static void main(String[] args) {
Account a = new Account();
a.create("张三");
a.deposit(100);
a.withdraw(50);
a.getBalance();
}
}