찾다

 >  Q&A  >  본문

急,三道Java题(附英文原题),我不知道我的答案对不对,也不知道怎么改正

1.假设我们有2个类,如下所述:
class Pet是父类,Dog是从Pet继承的子类。
每个类声明并创建一个新的私有变量,除了它继承的任何变量(这意味着Pet类创建了变量age,类Dog创建了变量name)。
每个类都创建以下方法:
一个 set()方法设置类可以访问的所有变量
一个 toString()方法来写出关于类的实例的所有数据的信息
和一个equals()方法,执行检查两个对象是否具有相同数据的常规操作
所有类都有一个构造函数 - 一个参数化的(初始化所有类数据)
为了最小化代码重复,并利用在已创建的方法中执行的任何错误检查,Class Dog中的每个方法将调用其他已创建的方法
这些是每个类中唯一的方法。

编写类Dog的类定义文件的Java代码。

2.假设一个Cat类已经为你创建,并继承自Pet。设计和编码一个main来测试Pet,class Dog和class Cat通过创建一个一维数组与两个Pet,2 Dog和2 Cat。编写一些代码,将显示当前存储在数组中每个对象中的数据。然后写一些代码来测试数组中的Dog对象可以访问的所有剩余方法。

3.对于问题#1中描述的Pet和Dog类,识别此程序段中的任何编译器或运行时错误,并尽可能修复它们。解释每个错误是什么,以及它为什么会发生。一旦你有这个程序的运行版本,它会产生什么输出?为什么会生成该输出?
宠物p

Dog d = new Dog();

p = d;
p.set(“Fido”,2);
p.age = 3;
SOP(p);


d = new Pet(4);
SOP(d);

第一题:

public class Dog extends Pet{

    private String name;

     public Dog(int theAge, String theName){
     super(theAge);
     name=theName;
     }
     public void setName(String theName){
     name=theName;
     }
     public String getName(){
     return name;
     }
     public String toString(){
     return "the pet's name is: "+name;
     }
     }

但是我不知道该怎样加 equals( ) method,而且set()后面是不是一定要加get(),如果不是,该怎么改正

第二题:

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

Dog dog1= new Dog(1,"dog1");
Dog dog2= new Dog(2,"dog2");

Cat cat1=new Cat(3,"cat1");
Cat cat2=new Cat(4,"cat2");

System.out.println (dog1.toString()+ ". "+dog1.getName()+"'s age is"+" "+ dog1.getAge());
System.out.println (dog2.toString()+ ". "+dog2.getName()+"'s age is"+" "+ dog2.getAge());
}
}

我想知道该怎么把getAge() 改成用toString()来表达

第三题:
看不懂。。。谁来帮忙解答一下

附上英文原题:
1.Suppose we have 2 classes as described below:
class Pet is the parent class, class Dog is the child class that inherits from Pet.
Each class declares and creates one new private variable, in addition to any variables it inherits (this means class Pet creates the variable age, class Dog creates the variable name).
Each class creates the following methods:
oa single set( ) method to set all variables the class has access to
oa toString( ) method to write out information about all data for the instance of the class
oand an equals( ) method that performs the usual operation of checking if two objects have the same data
All classes have one constructor— a parameterized one (that initializes all the class data)
to minimize code duplication, and to take advantage of any error checking done in already created methods, each method in Class Dog will call other already created methods where possible
These are the only methods in each class.

Write the Java code for the class definition file for class Dog. [4 marks]

2.Suppose that a Cat class has already been created for you, and inherits from Pet. Design and code a main to test the Pet, Dog and Cat classes by creating a one dimensional array with 2 Pets, 2 Dogs and 2 Cats. Write some code that will display the data currently stored in each object in the array. Then write some code to test all remaining methods that the Dog objects in the array have access to. [3 Marks]

3.For the Pet and Dog classes described in Question #1, identify any compiler or run-time errors in this program segment, and fix them if possible. Explain what each error is, and why it occurs. Once you have a running version of this program, what output will it produce? Why will it generate that output? [3 marks]

   Pet p;
   Dog d = new Dog( );
    
    p = d;
    p.set("Fido", 2);
    p.age = 3;
    SOP(p );

   d = new Pet( 4 );
  SOP(d);
PHP中文网PHP中文网2794일 전561

모든 응답(0)나는 대답할 것이다

답장 없음
  • 취소회신하다