实验三 类与对象
1 - [OJ2027]类与对象1
声明 Patient 类表示在门诊室中的病人, 具体要求如下:
(1)此类对象应包括: name(a String)、 sex(a char)、 age( an int)、weight(a float)、 allergies(a boolean)等属性;
(2)编写属性对应的 getter/setter 方法;
(3)声明并测试 toString()方法,用该方法显示一个病人的 name, sex, age, allergies 属性。
完成 Patient 的定义后,另创建一个测试类Main,在该类中生成两个 Patient 对象,输入对应的属性值后,直接打印输出该对象,测试验证toString()方法。
请参照如下程序框架编写程序:
import java.util.Scanner;
class Patient{
//定义属性
......
//定义getter/setter方法
......
//定义toString()方法
......
}
public class Main {
public static void main(String[] args) {
//创建两格Patient对象,输入对应的属性值
......
//分别打印输出该对象
......
}
}
import java.util.Scanner;
class Patient {
//定义属性
private String name;
private char sex;
private int age;
private float weight;
private boolean allergies;
//定义getter/setter方法
public void setName (String name){
this.name = name;
}
public void setSex (char sex){
this.sex = sex;
}
public void setAge (int age){
this.age = age;
}
public void setWeight (float weight){
this.weight = weight;
}
public void setAllergies (boolean allergies){
this.allergies = allergies;
}
public String getName (){
return this.name;
}
public char getSex (){
return this.sex;
}
public int getAge (){
return this.age;
}
public float getWeight (){
return this.weight;
}
public boolean getAllergies (){
return this.allergies;
}
//定义toString()方法
public String toString(){
return name + " " + age + " " + sex + " " + allergies;
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//创建两个Patient对象,输入对应的属性值
Patient p1 = new Patient();
Patient p2 = new Patient();
p1.setName(input.nextLine());
p1.setSex(input.next().charAt(0));
p1.setAge(input.nextInt());
p1.setWeight(input.nextFloat());
p1.setAllergies(input.nextBoolean());
input.nextLine();
p2.setName(input.nextLine());
p2.setSex(input.next().charAt(0));
p2.setAge(input.nextInt());
p2.setWeight(input.nextFloat());
p2.setAllergies(input.nextBoolean());
//分别打印输出该对象
System.out.println(p1);
System.out.println(p2);
// System.out.println("Name: " + p1.getName());
// System.out.println("Sex: " + p1.getSex());
// System.out.println("Age: " + p1.getAge());
// System.out.println("Weight: " + p1.getWeight());
// System.out.println("Allergies: " + p1.getAllergies());
}
}
2 - [OJ2028]类与对象2
按以下要求定义一个类 Circle 描述一个圆,并完成相应的操作:
import java.util.Scanner;
class Circle{
//1. 实例属性:圆心 xpoint、ypoint、半径 radius
//2. 构造方法:给3个属性赋初始值
//3. 实例方法: area,求圆的面积,圆周率用 Math.PI 表示
//4. 实例方法: circumference,求圆的周长
//5. 重写toString()方法,返回圆心坐标和半径,格式如:(1.5,3.6) 3.0
}
public class Main {
public static void main(String[] args) {
//1. 输入圆心坐标和半径
//2. 创建Circle对象
//3. 打印输出该对象
//4. 输出圆的面积和周长,保留2位小数
}
}
输入圆心坐标和半径。
分两行输出,第1行打印输出Circle对象,
第2行分别输出圆的面积和周长,保留2位小数。
import java.util.Scanner;
class Circle{
//1. 实例属性:圆心 xpoint、ypoint、半径 radius
private double xpoint;
private double ypoint;
private double radius;
//2. 构造方法:给3个属性赋初始值
public Circle(double xpoint, double ypoint, double radius){
this.xpoint = xpoint;
this.ypoint = ypoint;
this.radius = radius;
}
//3. 实例方法: area,求圆的面积,圆周率用 Math.PI 表示
public double area(double radius){
return Math.PI * radius * radius;
}
//4. 实例方法: circumference,求圆的周长
public double circumference(double radius){
return 2 * Math.PI * radius;
}
//5. 重写toString()方法,返回圆心坐标和半径,格式如:(1.5,3.6) 3.0
@Override
public String toString(){
return "(" + xpoint + "," + ypoint + ") " + radius;
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//1. 输入圆心坐标和半径
double xpoint = input.nextDouble();
double ypoint = input.nextDouble();
double radius = input.nextDouble();
//2. 创建Circle对象
Circle circle = new Circle(xpoint, ypoint, radius);
//3. 打印输出该对象
System.out.println(circle);
//4. 输出圆的面积和周长,保留2位小数
System.out.printf("%.2f %.2f", circle.area(radius), circle.circumference(radius));
}
}
3 - [OJ2029]类与对象3
按如下程序框架设计一个矩形类 Rectangle,具体要求如下:
import java.util.Scanner;
class Rectangle{
//1. 实例属性包括 a(a float)和 b(a float);
//2. 设计类的构造方法,包括无参构造方法和带参数的构造方法;
//3. 设计方法计算矩形的周长;
//4. 设计方法计算矩形的面积;
//5. 设计方法将矩形的长和宽都扩大 2 倍;
//6.设计一个类方法,使用可变长参数,在多个 Rectangle 对象中找出面积最大的对象,并返回最大面积。方法格式如下:
public static float GetMaxRectangle(Rectangle... objs)
{
// ......
}
}
public class Main {
public static void main(String[] args)
{
//1.数组3组数据,每组两个实数,分别代表长方形的两个边长
//2.调用构造方法创建3个Rectangle对象
//3.将第1个Rectangle对象 和 第3个Rectangle的长宽都扩大2倍
//4.分别输出3个Rectangle对象的面积和周长,保留2位小数
//5.调用GetMaxRectangle方法输出3个对象中面积的最大值,保留2位小数
}
}
输入3行数据,每行两个实数,分别代表长方形的两个边长。
执行程序框架指定顺序的操作后,分三行输出输出3个Rectangle对象的面积和周长,保留2位小数,然后继续在下一行输出最大面积。
import java.util.Scanner;
class Rectangle{
//1. 实例属性包括 a(a float)和 b(a float);
private float a;
private float b;
//2. 设计类的构造方法,包括无参构造方法和带参数的构造方法;
public Rectangle(){
this.a = 0.0f;
this.b = 0.0f;
}
public Rectangle(float a, float b){
this.a = a;
this.b = b;
}
//3. 设计方法计算矩形的周长;
public float circumference(){
return 2 * (a + b);
}
//4. 设计方法计算矩形的面积;
public float area(){
return a * b;
}
//5. 设计方法将矩形的长和宽都扩大 2 倍;
public void enlarge(){
this.a *= 2;
this.b *= 2;
}
//6.设计一个类方法,使用可变长参数,在多个 Rectangle 对象中找出面积最大的对象,并返回最大面积。方法格式如下:
public static float GetMaxRectangle(Rectangle... objs)
{
float maxArea = 0.0f;
for (Rectangle i : objs){
if (i.area() > maxArea){
maxArea = i.area();
}
}
return maxArea;
}
}
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
//1.数组3组数据,每组两个实数,分别代表长方形的两个边长
float[] a = new float[5];
float[] b = new float[5];
for (int i = 0; i < 3; i ++){
a[i] = input.nextFloat();
b[i] = input.nextFloat();
}
//2.调用构造方法创建3个Rectangle对象
Rectangle r1 = new Rectangle(a[0], b[0]);
Rectangle r2 = new Rectangle(a[1], b[1]);
Rectangle r3 = new Rectangle(a[2], b[2]);
//3.将第1个Rectangle对象 和 第3个Rectangle的长宽都扩大2倍
r1.enlarge();
r3.enlarge();
//4.分别输出3个Rectangle对象的面积和周长,保留2位小数
System.out.printf("%.2f %.2f\n", r1.area(), r1.circumference());
System.out.printf("%.2f %.2f\n", r2.area(), r2.circumference());
System.out.printf("%.2f %.2f\n", r3.area(), r3.circumference());
//5.调用GetMaxRectangle方法输出3个对象中面积的最大值,保留2位小数
System.out.printf("%.2f", Rectangle.GetMaxRectangle(r1, r2, r3));
}
}
Comments | NOTHING