6-1 汽车类
分数 15
全屏浏览题目
切换布局
作者 温彦
单位 山东科技大学
编写汽车类,其功能有启动(start),停止(stop),加速(speedup)和减速(slowDown),启动和停止可以改变汽车的状态(on/off),初始时状态为off,速度为0,speedUp和slowDown可以调整汽车的速度,每调用一次汽车速度改变10(加速增10,减速减10),汽车启动后才能加减速,加速上限为160,减速下限为0,汽车速度减为0后才能停止,给出汽车类的定义。
Main函数中构造一个汽车对象,并对该对象进行操作,各个操作的编号为:
- start
- stop
- speedup
- slowdown
操作完成后打印出汽车的状态和速度。输入描述:
操作
输出描述:
汽车的状态和速度
裁判测试程序样例:
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); Car c = new Car(); for (int i=0;i<n;i++) { int a = s.nextInt(); switch (a) { case 1: c.start(); break; case 2: c.stop(); break; case 3: c.speedUp(); break; case 4: c.slowDown(); break; } } System.out.print(c.status + " "); System.out.println(c.speed); } } /* 你的代码被嵌在这里 */
输入样例:
在这里给出一组输入。例如:
8 1 3 3 4 3 4 4 2
输出样例:
在这里给出相应的输出。例如:
off 0
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
class Car { String status; int speed; Car() { status = "off"; speed = 0; } public void start() { status = "on"; } public void stop() { status = "off"; speed = 0; } public void speedUp() { if(speed<=160) { speed+=10; status = "on"; } else { speed = 160; status = "on"; } } public void slowDown() { if(speed>=10) { speed-=10; status = "on"; } else { speed = 0; status = "off"; } } }
6-2 Implement Book
分数 15
全屏浏览题目
切换布局
作者 殷伟凤
单位 浙江传媒学院
创建一个Book类。
拷贝如下的代码框架。该类定义了一些方法:获取书名、判断是否可用、借书和还书。然而,我们所提供的框架缺少这些方法的实现。请在方法体中填上合适的代码。
主方法用于测试这些方法,运行此程序,应该有如下的输出:
Title (should be The Da Vinci Code): The Da Vinci Code Borrowed? (should be false): false Borrowed? (should be true): true Borrowed? (should be false): false
提示:查看主方法看每个方法如何使用,然后在每个方法中填入代码。
程序框架如下:
class Book { String title; boolean borrowed; // Creates a new Book public Book(String bookTitle) { // Implement this method } // Marks the book as rented public void rented() { // Implement this method } // Marks the book as not rented public void returned() { // Implement this method } // Returns true if the book is rented, false otherwise public boolean isBorrowed() { // Implement this method } // Returns the title of the book public String getTitle() { // Implement this method } }
仔细阅读测试程序中的main()方法,根据上述的样例框架补充完整缺失的方法实现部分。
裁判测试程序样例:
public class Main { public static void main(String[] arguments) { // Small test of the Book class Book example = new Book("The Da Vinci Code"); System.out.println("Title (should be The Da Vinci Code): " + example.getTitle()); System.out.println("Borrowed? (should be false): " + example.isBorrowed()); example.rented(); System.out.println("Borrowed? (should be true): " + example.isBorrowed()); example.returned(); System.out.println("Borrowed? (should be false): " + example.isBorrowed()); } } /* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
输出样例:
在这里给出相应的输出。例如:
Title (should be The Da Vinci Code): The Da Vinci Code Borrowed? (should be false): false Borrowed? (should be true): true Borrowed? (should be false): false
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
class Book { String title; boolean borrowed; public Book(String bookTitle) { this.title = bookTitle; borrowed = false; } public void rented() { borrowed = true; } public void returned() { borrowed = true; } public boolean isBorrowed() { if(borrowed == false) return false; else return true; } public String getTitle() { return title; } }
6-3 学生类
分数 20
全屏浏览题目
作者 温彦
单位 山东科技大学
有一个学生类的结构如下:
class Student { private int no; private String name; private int score; public Student(int _no, String _name, int _score) { no = _no; name = _name; score = _score; } public int getNo() {return no;} public String getName() {return name;} public int getScore() {return score;} public void print(){ System.out.println(no + " "+name+" "+score); } }
请构造main函数完成如下功能:
从键盘中读入三个学生的信息,比较他们的成绩,按照成绩由高到低排列输出输入描述:
三个学生的学号、姓名、成绩
输出描述:
由高到低排列输出的三个学生信息
裁判测试程序样例:
/*你的代码被嵌在这里*/ class Student { private int no; private String name; private int score; public Student(int _no, String _name, int _score) { no = _no; name = _name; score = _score; } public int getNo() {return no;} public String getName() {return name;} public int getScore() {return score;} public void print(){ System.out.println(no + " "+name+" "+score); } }
输入样例:
在这里给出一组输入。例如:
1 wang 89 2 liu 78 3 ma 90
输出样例:
在这里给出相应的输出。例如:
3 ma 90 1 wang 89 2 liu 78
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student> list = new ArrayList<>(); for(int i=0;i<3;i++) { int no = in.nextInt(); String name = in.next(); int score = in.nextInt(); Student stu = new Student(no,name,score); list.add(stu); } Collections.sort(list,(a,b)->b.getScore()-a.getScore()); for(Student s:list) { s.print(); } } }
6-4 设计银行Account类
分数 25
全屏浏览题目
作者 刘凤良
单位 天津仁爱学院
定义一个Account类,表示银行账户。Account类包括:
- int类型的私有数据域id,表示账号;String类型的私有数据域name,表示客户名;double类型的私有数据域balance,表示账户余额;double类型的私有数据域annualInterestRate,表示年利率。
- 有参构造方法,将id、name、balance和annualInterestRate设置为给定的参数。
- id、name、balance和annualInterestRate的更改器和访问器方法。
- 成员方法withdraw,从账户中取特定数额的款。
- 成员方法deposit,往账户中存特定数额的款。
- 成员方法getMonthlyInterestRate,返回月利率。
- 成员方法print,输出账户信息。
注意,Account类的定义应该这样开始:
class Account {
也就是说,Account类的class前面不要有public。。裁判测试程序样例:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int id = in.nextInt(); String name = in.next(); double balance = in.nextDouble(); double annualInterestRate = in.nextDouble(); Account account = new Account(id, name, balance, annualInterestRate); account.withdraw(2500); account.deposit(3000); account.print(); in.close(); } } //设计银行Account类 class Account { //TODO 编写代码 }
输入样例:
112233 ZhangSan 20000 4.5
输出样例:
112233 ZhangSan 20500.0 0.00375%
提示:
假设年利率为4.5%,则以4.5作为输入值。
月利率=年利率/1200
需使用 Main 作为主类名代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
class Account { private int id; private String name; private double balance; private double annualInterestRate; Account(int id,String name,double balance,double annualInterestRate) { this.id = id; this.name = name; this.balance = balance; this.annualInterestRate = annualInterestRate; } public void setId(int id) { this.id = id; } public int getId() { return id; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setBalance(double balance) { this.balance = balance; } public double getBalance() { return balance; } public void setannualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } public double getannualInterestRate() { return annualInterestRate; } public void withdraw(int m) { if(balance>=m) balance -= m; else balance = 0; } public void deposit(int m) { balance += m; } public double getMonthlyInterestRate() { return annualInterestRate/1200; } public void print() { System.out.println(id+"\n"+name+"\n"+balance+"\n"+this.getMonthlyInterestRate()+"%"); } }
7-1 MyDate类
分数 15
全屏浏览题目
作者 温彦
单位 山东科技大学
构造日期类MyDate类,包含年月日,提供相应的get和set函数,提供void print()函数打印日期,提供int compare(MyDate d)测试当前对象和参数对象d的早晚,如果早则返回-1,晚则返回1,相等则返回0
在main函数中,读入两个日期对象,输出第一个日期对象的信息,输出两个对象的比较结果输入格式:
两个日期对象,第一个为当前日期对象的年月日,第二个为待比较日期对象的年月日
输出格式:
当前日期对象的信息,当前对象和待比较日期对象的比较结果
输入样例:
在这里给出一组输入。例如:
2008 6 12 2009 6 22
输出样例:
在这里给出相应的输出。例如:
6/12/2008 -1
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
import java.util.Scanner; import java.util.ArrayList; class MyDate { private int year; private int month; private int date; MyDate(int year,int month,int date) { this.year = year; this.month = month; this.date = date; } public int compare(MyDate d) { if(this.year>d.year) { return 1; } else if(this.year==d.year && this.month>d.month) { return 1; } else if(this.year==d.year && this.month==d.month && this.date>d.date) { return 1; } else if(this.year==d.year && this.month==d.month && this.date==d.date) { return 0; } else { return -1; } } public void print() { System.out.print(date+"/"+month+"/"+year); } } public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<MyDate> list = new ArrayList<>(); for(int i=0;i<2;i++) { int year = in.nextInt(); int month = in.nextInt(); int date = in.nextInt(); MyDate mydate = new MyDate(year,month,date); list.add(mydate); } list.get(0).print(); System.out.print(" "+list.get(0).compare(list.get(1))); } }
7-2 Ring类设计
分数 10
全屏浏览题目
切换布局
作者 ami
单位 临沂大学
编写一个圆环类Ring的Java程序。
a定义圆环类的2个数据成员,分别是内半径innerRadius,外半径outerRadius,这些属性通过get和set方法进行封装。
b 定义圆环类有参构造方法Ring(int innerRadius,int outerRadius),在有参构造方法中加入System.out.println("constructor");
c完成无参构造方法Ring(),要求在无参构造方法中使用this调用有参构造方法给两个半径赋值(外半径赋值3,内半径赋值1)
d 圆环类中定义 public int getArea()方法可以返回其面积。面积求出后强制转换为整型值返回,π使用Math.PI表示。
在Main类中先生成一个圆环类对象,这个圆环的两个半径通过键盘读入,调用求面积方法求出面积后,输出面积。
然后再次定义一个圆环对象,调用无参构造方法,调用求面积方法求出面积后,输出面积。输入格式:
输入在一行中先给出内半径,再给出外半径。
输出格式:
在一行中输出圆环的面积。
输入样例:
在这里给出一组输入。先是内半径,然后是外半径,例如:
1 2
输出样例:
在这里给出相应的输出。例如:
constructor 9 constructor 25
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
import java.util.Scanner; class Ring { private int innerRadius; private int outerRadius; Ring(int innerRadius,int outerRadius) { this.innerRadius = innerRadius; this.outerRadius = outerRadius; System.out.println("constructor"); } Ring() { this.innerRadius = 1; this.outerRadius = 3; System.out.println("constructor"); } public void setInnerRadius(int innerRadius) { this.innerRadius = innerRadius; } public int getInnerRadius() { return innerRadius; } public void setOuterRadius(int outerRadius) { this.outerRadius = outerRadius; } public int getOuterRadius() { return outerRadius; } public int getArea() { return (int)(Math.PI*(Math.pow(outerRadius, 2)-Math.pow(innerRadius, 2))); //(int) a * b,强制类型转换的操作会在乘法之前将a变成整型,而不是将乘法的结果转换为整型,所以注意加括号 } } public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int outer = in.nextInt(); int inner = in.nextInt(); Ring r1 = new Ring(outer,inner); int area1 = r1.getArea(); System.out.println(area1); Ring r2 = new Ring(); int area2 = r2.getArea(); System.out.println(area2); } }