第三部分
题目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分数分别是90,80,70,打印输出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类,有个一个估值属性,构造函数初始化公司的估值
Car和Company都可以买保险,保险费车的是车龄乘以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,其中go(int 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,路况good,bad,满足
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,属性Teacher,Student[],构造函数初始化两个属性。
行为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个字符(n从1开始计数)
这是本题亲测结果:
提交的代码
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草草了事,东西是给自己学的、对自己负责;时间关系没有给大家做代码注释,希望同学们上课时候认真听讲、做到举一反三。
如有雷同纯属巧合、若有侵权及时联系删文