第一部分
第1题
题目描述:
有个接口Camera,包含一个方法void takePicture()。现在有类CellPhone和IWatch如下,Travel类中的snap方法,需要一个参数Carema,现在请改造Cellphone和IWatch类,满足测试样例的输出要求
这是本题亲测结果:
提交的代码
如下:
interface Camera
{
void takePicture();
}
class CellPhone implements Camera
{
String name;
public CellPhone(String name)
{
this.name=name;
}
public void takePicture()
{
System.out.printf("picture by %s cellphone\n",this.name);
}
}
class IWatch implements Camera
{
String name;
public IWatch(String name)
{
this.name=name;
}
public void takePicture()
{
System.out.printf("picture by %s iwatch\n",this.name);
}
}
class Travel
{
public void snap(Camera c){
c.takePicture();
}
}
public class Main {
public static void main(String[] args) {
CellPhone cp=new CellPhone("Huawei");
IWatch iw=new IWatch("Apple");
Travel travel=new Travel();
travel.snap(cp);
travel.snap(iw);
}
}
个人IDEA端调试代码
如果是在IDEA中调试应为以下代码(补齐OJ自带部分代码):
interface Camera
{
void takePicture();
}
class CellPhone implements Camera
{
String name;
public CellPhone(String name)
{
this.name=name;
}
public void takePicture()
{
System.out.printf("picture by %s cellphone\n",this.name);
}
}
class IWatch implements Camera
{
String name;
public IWatch(String name)
{
this.name=name;
}
public void takePicture()
{
System.out.printf("picture by %s iwatch\n",this.name);
}
}
class Travel
{
public void snap(Camera c){
c.takePicture();
}
}
public class Main {
public static void main(String[] args) {
CellPhone cp=new CellPhone("Huawei");
IWatch iw=new IWatch("Apple");
Travel travel=new Travel();
travel.snap(cp);
travel.snap(iw);
}
}
第2题
题目描述:
观察以下代码,在注释部分添加代码。
要求实例化一个Student对象,通过设置name和ID值,利用getStudentID()输出Tomcat123456
注:使用System.out.println()方法输出
这是本题亲测结果:
提交的代码
如下:
public class Test {
public static void main(String[] args) {
Student s1=new Student();
s1.setName("tomcat");
s1.setID(123456);
System.out.println(s1.getStudentID());
}
}
class Student{
private String name;
private int ID;
public void setName(String name){
this.name=name;
}
public void setID(int ID){
this.ID=ID;
}
public String getStudentID(){
return name+ID;
}
}
个人IDEA端调试代码
如果是在IDEA中调试应为以下代码(补齐OJ自带部分代码):
public class Main {
public static void main(String[] args) {
Student s1=new Student();
s1.setName("tomcat");
s1.setID(123456);
System.out.println(s1.getStudentID());
}
}
class Student{
private String name;
private int ID;
public void setName(String name){
this.name=name;
}
public void setID(int ID){
this.ID=ID;
}
public String getStudentID(){
return name+ID;
}
}
1
第3题
题目描述:
设计一个矩形类Rectangle,包含私有的数据成员宽度(Width)和高度(Height);公有的方法double getArea()返回矩形的面积,公有的方法double getPerimeter()返回矩形的周长。
这是本题亲测结果:
提交的代码
如下:
class Rectangle
{
double w,h;
Rectangle()
{
w=1;
h=1;
}
Rectangle(double w,double h)
{
this.w=w;
this.h=h;
}
double getArea()
{
return w*h;
}
double getPerimeter()
{
return (w+h)*2;
}
}
个人IDEA端调试代码
如果是在IDEA中调试应为以下代码(补齐OJ自带部分代码):
public class Main {
public static void main(String[] args) {
Rectangle rect=new Rectangle(8,5);
System.out.printf("The Area is %.2f,Perimeter is %.2f\n", rect.getArea(),rect.getPerimeter());
}
}
class Rectangle
{
double w,h;
Rectangle()
{
w=1;
h=1;
}
Rectangle(double w,double h)
{
this.w=w;
this.h=h;
}
double getArea()
{
return w*h;
}
double getPerimeter()
{
return (w+h)*2;
}
}
第4题
题目描述:
设计一个名为Person的类和它的两个名为Student和Employee的子类。Person类有私有成员变量:姓名、地址、电话号码和邮箱。Student类有私有成员变量:年级,Employee类有私有成员变量:部门和工资。要求各个类都定义带参数的构造方法,对私有数据成员进行初始化。各个类中定义公有的show()方法,输出该类对象的成员变量值。
说明:成员变量“工资”为 int 类型
这是本题亲测结果:
提交的代码
如下:
public class Person{
private String n;
private String a;
private String p;
private String e;
public String getn(){
return n;
}
public String geta(){
return a;
}
public String getp(){
return p;
}
public String gete(){
return e;
}
public Person(String n,String a,String p,String e){
super();
this.n=n;
this.a=a;
this.p=p;
this.e=e;
}
public void show(){
System.out.println(getn()+","+geta()+","+getp()+","+gete());
}
}
public class Student extends Person{
private String nianji;
public String getnianji(){
return nianji;
}
public Student(String n,String a,String p,String e,String nianji){
super(n,a,p,e);
this.nianji=nianji;
}
public void show(){
System.out.println(getn()+","+geta()+","+getp()+","+gete()+","+getnianji());
}
}
public class Employee extends Person{
private String b;
private int g;
public String getb(){
return b;
}
public int getg(){
return g;
}
public Employee(String n,String a,String p,String e,String b,int g){
super(n,a,p,e);
this.b=b;
this.g=g;
}
public void show(){
System.out.println(getn()+","+geta()+","+getp()+","+gete()+","+getb()+","+getg());
}
}
个人IDEA端调试代码
如果是在IDEA中调试应为以下代码(补齐OJ自带部分代码):
class Person{
private String n;
private String a;
private String p;
private String e;
public String getn(){
return n;
}
public String geta(){
return a;
}
public String getp(){
return p;
}
public String gete(){
return e;
}
public Person(String n,String a,String p,String e){
super();
this.n=n;
this.a=a;
this.p=p;
this.e=e;
}
public void show(){
System.out.println(getn()+","+geta()+","+getp()+","+gete());
}
}
class Student extends Person{
private String nianji;
public String getnianji(){
return nianji;
}
public Student(String n,String a,String p,String e,String nianji){
super(n,a,p,e);
this.nianji=nianji;
}
public void show(){
System.out.println(getn()+","+geta()+","+getp()+","+gete()+","+getnianji());
}
}
class Employee extends Person{
private String b;
private int g;
public String getb(){
return b;
}
public int getg(){
return g;
}
public Employee(String n,String a,String p,String e,String b,int g){
super(n,a,p,e);
this.b=b;
this.g=g;
}
public void show(){
System.out.println(getn()+","+geta()+","+getp()+","+gete()+","+getb()+","+getg());
}
}
public class Main {
public static void main(String[] args) {
Person p=new Person("john","qingdao","12345678900","john@upc.edu.cn");
p=new Student("mike","guangdong","13844448888","mike@163.com","Sophomore");
p.show();
p=new Employee("peter","hongkong","18555556666","peter@163.com","computer",12000);
p.show();
}
}
第5题
题目描述:
现有Poeple类,如下所示:
public class People{
public String name;
public String birthday;
public void setBirthday(String birthday){ //要求日期格式 1988-01-01
this.birthday=birthday;
}
public int getAge(){
String y=birthday.substring(0,4);
Calendar c=Calendar.getInstance();
return c.get(Calendar.YEAR)-Integer.parseInt(y);
}
}
现在设计一个Manager类作为People的子类,在Manager中定义一个方法int getBonus(int x),通过调用父类的getAge方法,获取年龄,返回奖金值为年龄的x倍。
设计一个Employee类作为People的子类,在类Employee中定义一个方法int getWorkday(),通过调用父类的getAge方法,获取年龄,返回工作日为当年天数,减去115法定休息日,再减去职工年龄。
这是本题亲测结果:
提交的代码
如下:
import java.util.Calendar;
public class People{
public String name;
public String birthday;
public void setBirthday(String birthday){
this.birthday=birthday;
}
public int getAge(){
String y=birthday.substring(0,4);
Calendar c=Calendar.getInstance();
return c.get(Calendar.YEAR)-Integer.parseInt(y);
}
}
class Manager extends People{
public int getBonus(int x){
return x*(super.getAge());
}
}
class Employee extends People{
public int getWorkday(){
return 366-115-super.getAge()-1;
}
}
个人IDEA端调试代码
如果是在IDEA中调试应为以下代码(补齐OJ自带部分代码):
import java.util.Calendar;
class People{
public String name;
public String birthday;
public void setBirthday(String birthday){
this.birthday=birthday;
}
public int getAge(){
String y=birthday.substring(0,4);
Calendar c=Calendar.getInstance();
return c.get(Calendar.YEAR)-Integer.parseInt(y);
}
}
class Manager extends People{
public int getBonus(int x){
return x*(super.getAge());
}
}
class Employee extends People{
public int getWorkday(){
return 366-115-super.getAge();//因为后台系统参数问题,提交的时候要加一个“-1”
}
}
public class Main {
public static void main(String[] args) {
Manager m=new Manager();
m.setBirthday("1990-05-01");
System.out.printf("%d\n",m.getBonus(10));
Employee e=new Employee();
e.setBirthday("1996-03-20");
System.out.printf("%d\n",e.getWorkday());
}
}
写在最后
以上代码仅供同学们参考,切勿复制粘贴、1草草了事,东西是给自己学的、对自己负责;时间关系没有给大家做代码注释,希望同学们上课时候认真听讲、做到举一反三。
如有雷同纯属巧合、若有侵权及时联系删帖