Java程序设计课程 UPC面向对象程序设计(Java)编程练习题集(三)

第三部分

题目1:

题目描述:

设计一个圆类Circle,包含私有的成员变量radius(半径);公有的构造方法,对radius进行初始化;公有的方法double getArea(),获取圆的面积;公有的方法double getPerimeter(),获取圆的周长。

设计一个圆柱体类Cylinder,继承Circle类,还包含私有的成员变量height(圆柱体的高);公有的方法double getVolume(),获取圆柱体体积。

说明:圆周率πMath类中的类常量PI,即Math.PI 

这是本题亲测结果:

提交的代码

class Circle{

    private int radius;

    public Circle(int r){

        radius = r;

    }

    public double getArea(){

        return radius * radius * Math.PI;

    }

    public double getPerimeter(){

        return 2 * Math.PI * radius;

    }

}

class Cylinder extends Circle{

    private int height;

    public Cylinder(int r, int h) {

        super(r);

        height = h;

    }

    public double getVolume(){

        return getArea() * height;

    }

}

个人IDEA端调试代码

如果是在IDEA中调试应为以下代码(补齐OJ自带部分代码):

class Circle{
    private int radius;
    public Circle(int r){
        radius = r;
    }
    public double getArea(){
        return radius * radius * Math.PI;
    }
    public double getPerimeter(){
        return 2 * Math.PI * radius;
    }
}

class Cylinder extends Circle{
    private int height;
    public Cylinder(int r, int h) {
        super(r);
        height = h;
    }
    public double getVolume(){
        return getArea() * height;
    }
}

public class Main {
    public static void main(String[] args) {

        Circle c=new Cylinder(3,3);
        System.out.printf("The circle`s area is:%.2f,perimeter is:%.2f.\n",c.getArea(),c.getPerimeter());
        System.out.printf("The cylinder`s volume is:%.2f.\n",((Cylinder)c).getVolume());
    }
}

题目2:

题目描述:

设计一个名为First的类,实例化后,能够输出Hello World!

这是本题亲测结果:

提交的代码

class First{

    public First(){

        System.out.print("Hello World!");

    }

}

public class Main {

    public static void main(String[] args) {

        new First();

    }

}

个人IDEA端调试代码

如果是在IDEA中调试应为以下代码(补齐OJ自带部分代码):

class First{
    public First(){
        System.out.print("Hello World!");
    }
}
public class Main {
    public static void main(String[] args) {
        new First();
    }
}



题目3:

题目描述:

一个Student类,类的结构如下,要求将你的姓名和学号作为参数对该类进行实例化,实例化的对象名为student,通过setScore方法录入数学,英语和java分数分别是908070,打印输出getAverage()方法获取的平均分

public class Student{

   String name;

   String id;

    public Student(String name,String id){}

    public void setScore(float math,float english, float java) {}

    public float getAverage(){}

}

这是本题亲测结果:

提交的代码

Student student = new Student("wjh", "2107010218");//这个题这俩字符串你改成你自己的信息。

student.setScore(90, 80, 70);

System.out.println(student.getAverage());

题目4:

题目描述:

现有一个类Game,其中一个方法是void input(int i),调用该方法时,如果输入的数据是7会抛出异常。现要求设计一个类Person,包括一个void play(int number)方法,该方法中,要求实例化Game,并调用input方法,将变量number作为参数输入到input方法中。如果出现异常,输出error

这是本题亲测结果: 

提交的代码 

class Game{

    public void input(int i) throws Exception{

        if(i==7){

            throw new Exception();

        }else{

            System.out.println(i);

        }

    }

}

class Person{

    void play(int number){

        Game game = new Game();

        try {

            game.input(number);

        } catch (Exception e) {

            System.out.println("error");

        }

    }

}

个人IDEA端调试代码

如果是在IDEA中调试应为以下代码(补齐OJ自带部分代码):

class Game{
    public void input(int i) throws Exception{
        if(i==7){
            throw new Exception();
        }else{
            System.out.println(i);
        }
    }
}
class Person{
    void play(int number){
        Game game = new Game();
        try {
            game.input(number);
        } catch (Exception e) {
            System.out.println("error");
        }
    }
}

public class Main {
    public static void main(String[] args) {

        Person p=new Person();
        p.play(5);

    }
}



题目5:

题目描述:

设计一个Car类,有一个车龄属性,构造函数初始化车龄

设计一个Company类,有个一个估值属性,构造函数初始化公司的估值

CarCompany都可以买保险,保险费车的是车龄乘以100,公司是估值除以100

设计一个Insurable接口,包括一个抽象方法getFee();

现在有个系统已经实现的People类中包含一个方法display(Insuable s),能够打印出输入参数的费用

根据下面测试样例的调用方式,你将如何设计?请完成类和接口的设计

这是本题亲测结果: 

提交的代码

class People{

    public static void display(Insurable s){

        System.out.println(s.getFee());

    }

}

class Car implements Insurable{

    int age;

    public Car(int age){

        this.age = age;

    }

    public double getFee() {

        return age * 100;

    }

}

class Company implements Insurable{

    int value;

    public Company(int value){

        this.value = value;

    }

    public double getFee() {

        return value / 100;

    }

}

interface Insurable{

    double getFee();

}

个人IDEA端调试代码

如果是在IDEA中调试应为以下代码(补齐OJ自带部分代码):

class People{
    public static void display(Insurable s){
        System.out.println(s.getFee());
    }
}
class Car implements Insurable{
    int age;
    public Car(int age){
        this.age = age;
    }
    public double getFee() {
        return age * 100;
    }
}
class Company implements Insurable{
    int value;
    public Company(int value){
        this.value = value;
    }
    public double getFee() {
        return value / 100;
    }
}
interface Insurable{
    double getFee();
}

public class Main {
    public static void main(String[] args) {

        Car p=new Car(20);
        Company c=new Company(1000000);
        People.display(p);
        People.display(c);
    }
}

题目6:

题目描述:

设计一个类Travel,其中goint mile)方法实例化SchoolBus类,调用SchoolBus类的getSpeed()方法,获取旅行时间

注:class SchoolBus{

    public int getSpeed(){}

    public int getSpeed(String weather){}// weather is "sun" or "rain"

}

这是本题亲测结果:

提交的代码

class Travel{

    double go(int mile){

        SchoolBus sb = new SchoolBus();

        return mile / sb.getSpeed();

    }

}

题目7:

题目描述:

设计一个Car类,getSpeed(String Weather, String roadCondition)方法,天气有 sun,rain,路况goodbad,满足

sun good 120

sun bad  100

rain good 90

rain bad 70

这是本题亲测结果: 

提交的代码

class Car{

    public int getSpeed(String Weather, String roadCondition){

        if(Weather.equals("sun")){

            if(roadCondition.equals("good")){

                return 120;

            }

            else{

                return 100;

            }

        }

        else{

            if(roadCondition.equals("good")){

                return 90;

            }

            else{

                return 70;

            }

        }

    }

}

个人IDEA端调试代码

如果是在IDEA中调试应为以下代码(补齐OJ自带部分代码):

class Car{
    public int getSpeed(String Weather, String roadCondition){
        if(Weather.equals("sun")){
            if(roadCondition.equals("good")){
                return 120;
            }
            else{
                return 100;
            }
        }
        else{
            if(roadCondition.equals("good")){
                return 90;
            }
            else{
                return 70;
            }
        }
    }
}

public class Main {
    public static void main(String[] args) {

        Car c=new Car();
        System.out.println(c.getSpeed("sun","good"));
    }
}

题目8:

题目描述:

某科学生成绩在数据库提取出成字符串如下格式:

“Tom:85,Jack:70,Black:95”

设计一个方法int getMax(String s),返回字符串s中的最高分

这是本题亲测结果: 

提交的代码

class MyUtil{

    public int getMax(String s){

        int mx = 0;

        String[] array = s.split(",");

        for(String i:array){

            String[] say = i.split(":");

            if(mx < Integer.parseInt(say[1])){

                mx = Integer.parseInt(say[1]);

            }

        }

        return mx;

    }

}

个人IDEA端调试代码

如果是在IDEA中调试应为以下代码(补齐OJ自带部分代码):

class MyUtil{

    public int getMax(String s){
        int mx = 0;
        String[] array = s.split(",");
        for(String i:array){
            String[] say = i.split(":");
            if(mx < Integer.parseInt(say[1])){
                mx = Integer.parseInt(say[1]);
            }
        }
        return mx;
    }

}

public class Main {
    public static void main(String[] args) {

        MyUtil mu=new MyUtil();
        String s="Tom:85,Jack:70,Black:95";
        System.out.println(mu.getMax(s));
    }
}

题目9:

题目描述:

①定义类Teacher,属性name,subject,行为teach,构造函数初始化两个属性。

      ②定义类Student,属性name,grade,行为study,构造函数初始化两个属性。

      ③定义类ClassRoom,属性TeacherStudent[],构造函数初始化两个属性。

    行为showMembers()输出教师和学生的名字

这是本题亲测结果: 

提交的代码

class Teacher{

    String name,subject;

    public Teacher(String name, String subject){

        this.name = name;

        this.subject = subject;

    }

    public void teach(){}

}

class Student{

    String name;

    int grade;

    public Student(String name, int grade){

        this.name = name;

        this.grade = grade;

    }

    public void study(){}

}

class ClassRoom{

    Teacher teacher;

    Student[] students;

    public ClassRoom(Teacher teacher, Student[] students){

        this.teacher = teacher;

        this.students = students;

    }

    public void showMembers(){

        System.out.print(teacher.name);

        for(Student i:students){

            System.out.print("," + i.name);

        }

    }

}

个人IDEA端调试代码

如果是在IDEA中调试应为以下代码(补齐OJ自带部分代码):

class Teacher{
    String name,subject;
    public Teacher(String name, String subject){
        this.name = name;
        this.subject = subject;
    }
    public void teach(){}
}

class Student{
    String name;
    int grade;
    public Student(String name, int grade){
        this.name = name;
        this.grade = grade;
    }
    public void study(){}
}

class ClassRoom{
    Teacher teacher;
    Student[] students;
    public ClassRoom(Teacher teacher, Student[] students){
        this.teacher = teacher;
        this.students = students;
    }
    public void showMembers(){
        System.out.print(teacher.name);
        for(Student i:students){
            System.out.print("," + i.name);
        }
    }
}

public class Main {
    public static void main(String[] args) {

        Teacher t=new Teacher("ljh","java");
        Student[] s={new Student("tom",80),new Student("jack",90),new Student("xiaoming",100)};
        ClassRoom cr=new ClassRoom(t,s);
        cr.showMembers();
    }
}



题目10:

题目描述:

设计一个类Computer,一个名为publpub(int n)的方法,计算1~n的累加和并输出。

1+2+3+……+n

这是本题亲测结果: 

提交的代码

class Computer{

    public int getTotal(int n){

        return (1+n)*n/2;

    }

}

个人IDEA端调试代码

如果是在IDEA中调试应为以下代码(补齐OJ自带部分代码):

class Computer{
    public int getTotal(int n){
        return (1+n)*n/2;
    }
}

public class Main {
    public static void main(String[] args) {


        Computer c=new Computer();
        System.out.println(c.getTotal(100));
    }
}



题目11:

题目描述:

有个People类如下,设计一个子类Student,继承People类,增加一个public void study()方法,让wealthValue增加1.重写父类的play()方法,让财富值-2.

class People{

    public int wealthValue=100;

    public String name;

    public void eat(){

        wealthValue--;

    }

    public void play(){

        wealthValue--;

    }

    public int getWealthValue(){

        return wealthValue;

    }

}

这是本题亲测结果: 

提交的代码

class Student extends People{

    public void study(){

        wealthValue++;

    }

    public void play(){

        wealthValue -= 2;

    }

}

个人IDEA端调试代码

如果是在IDEA中调试应为以下代码(补齐OJ自带部分代码):

class People{

    public int wealthValue=100;

    public String name;

    public void eat(){

        wealthValue--;

    }

    public void play(){

        wealthValue--;

    }

    public int getWealthValue(){

        return wealthValue;

    }

}
class Student extends People{
    public void study(){
        wealthValue++;
    }
    public void play(){
        wealthValue -= 2;
    }
}

public class Main {
    public static void main(String[] args) {
        Student s=new Student();
        s.eat();
        s.play();
        s.study();
        s.study();
        s.study();
        System.out.println(s.getWealthValue());
    }
}



题目12:

题目描述:

创建一个名为MyFileReader的类,设计一个方法 char read(String fileName, int n),该读取名为fileName的文件,返回文件中第n个字符(n1开始计数)

这是本题亲测结果: 

提交的代码

import java.io.FileReader;

import java.io.IOException;

class MyFileReader{

    public char read(String fileName, int n){

        try {

            FileReader reader = new FileReader(fileName);

            char[] c = new char[n+1];

            reader.read(c);

            reader.close();

            return c[n];

        } catch (IOException e) {

            throw new RuntimeException(e);

        }

    }

}

题目13:

题目描述:

设计一个银行类MyBank,初始化时没有钱,saveMoney方法实现存钱,参数是float类型,withdrawMoney取钱,参数是float类型,取钱需要付1元手续费,如果取钱数超过存款数,取钱操作失败,返回“not enough”getDeposit方法获取当前存款数

这是本题亲测结果: 

提交的代码

class MyBank{

    float money = 0;

    public void saveMoney(float sm){

        money += sm;

    }

    public void withdrawMoney(float wm){

        if(money - 1 < wm){

            System.out.println("not enough");

        }

        else{

            money = money - 1 - wm;

        }

    }

    public float getDeposit(){

        return money;

    }

}

个人IDEA端调试代码

如果是在IDEA中调试应为以下代码(补齐OJ自带部分代码):

class MyBank{
    float money = 0;
    public void saveMoney(float sm){
        money += sm;
    }
    public void withdrawMoney(float wm){
        if(money - 1 < wm){
            System.out.println("not enough");
        }
        else{
            money = money - 1 - wm;
        }
    }
    public float getDeposit(){
        return money;
    }
}


public class Main {
    public static void main(String[] args) {
       
        MyBank bank=new MyBank();
        bank.saveMoney(100);
        bank.withdrawMoney(80);
        System.out.println(bank.getDeposit());
       
    }
}

写在最后

以上代码仅供同学们参考,切勿复制粘贴、1草草了事,东西是给自己学的、对自己负责;时间关系没有给大家做代码注释,希望同学们上课时候认真听讲、做到举一反三。

如有雷同纯属巧合、若有侵权及时联系删文

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云边牧风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值