Java基础编程题

package cn.ALAN_CF.studyf001;
/**
 * 设计一个名为Rectangle的类表示矩形。这个类包括:
 * 两个名为width和height的double型数据域,它们分别表示矩形的宽和高。
 * width和height的默认值都为1.
 * 一个无参构造方法。 一个为width和height指定值的矩形构造方法。
 * 一个名为getArea()的方法返回这个矩形的面积。
 * 一个名为getPerimeter()的方法返回这个矩形的周长。
 * @author 15328
 */
public class Rectangle {
    double width = 1;
    double height = 1;
    Rectangle(){

    }
    Rectangle(double width,double height) {
        this.width = width;
        this.height = height;
    }
    public double getArea(){
        return width * height;
    }
    public double getPerimeter(){
        return 2 * (width + height);
    }
    public static void main(String[] args) {
        Rectangle exampleRectangle1=new Rectangle(12.5,16.0);
        Rectangle exampleRectangle2 = new Rectangle();
        System.out.println("example1.area:" + exampleRectangle1.getArea());
        System.out.println("example1.perimeter: "+exampleRectangle1.getPerimeter());
        System.out.println("example2.area: "+exampleRectangle2.getArea());
        System.out.println("exampleRectangle2.Perimeter: "+exampleRectangle2.getPerimeter());
    }
}
package cn.ALAN_CF.studyf001;
import java.util.Scanner;

/**
 * 请定义一个学生类(Student),包含学号、姓名、年龄(7-60岁)三个私有成员。
 * 从键盘输入学生的成员值后生成对象,并按要求输出相应的结果。
 * 输入格式:
 * 第一行一个整数k,代表后面要生成的学生人数。
 * 从第二行开始的连续k行,每行3个数据,分别表示一个学生的学号、姓名和年龄。
 * 输出格式:
 * 输出每个学生的基本情况。如果数据不符合要求则输出"bad"
 * @author 15328
 */
class Studenttype{
    private int id;
    private String name;
    private int age;
    Studenttype (int id,String name,int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public void output() {
        if(age<7||age>70) {
            System.out.print("bad");
        }
        else {
            System.out.print(Integer.toString(id)+' '+name+' '+Integer.toString(age));
        }
    }
}
/**
 * @author 15328
 */
public class Student {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int count;
        count = Integer.parseInt(input.nextLine());
        int id,age;
        String name;
        String Line;
        String[] Line_p = new String[3];
        Studenttype[] stu = new Studenttype[count];
        for(int i = 0; i < count; i++) {
            Line = input.nextLine();
            Line_p = Line.split(" ");
            id = Integer.parseInt(Line_p[0]);
            name = Line_p[1];
            age = Integer.parseInt(Line_p[2]);
            stu[i] = new Studenttype(id, name, age);
        }
        for(int i = 0; i < count; i++) {
            stu[i].output();
            if(i!=count-1) {
                System.out.println();
            }
        }
    }
}

posted @ 2023-05-04 11:53  在天边偷看小天使  阅读(10)  评论(0)    收藏  举报  来源