?
Solved

a simple program

Posted on 2005-03-12
9
Medium Priority
?
215 Views
Last Modified: 2010-03-31
Access modifier          Description of some methods in the above diagram
#  => protected          + walk()     => takes in no argument, returns no value
+  => public               + getName() : String  => takes in no arguments, returns a
-  => private                                   String value

                    + setName( name:String)  => takes in one String
     argument, returns no value


Question 1 [ 40 marks ]

Based on the above diagram,

a)     Create the Animal class, which is the abstract superclass of all animals.
[ 2 marks ]
i.     Declare a protected integer attribute called legs, which records the number of legs for this animal.                    [ 2 marks ]
ii.     Define a protected constructor that initializes the legs attribute
[ 2 marks ]
iii.     Declare an abstract method eat.                    [ 2 marks ]
iv.     Declare a concrete method walk that displays how the animal walks, including the number of legs.                    [ 3 marks ]

b)     Create the Cat class that inherits from the Animal class.          [ 2 marks ]

i.     This class has a String attribute to store the name of the pet.
[ 2 marks ]
ii.     Define a constructor that takes a String parameter that specifies the cat’s name. This constructor must also call the superclass constructor to specify that all cats have four legs.               [ 4 marks ]
iii.     Overload the constructor so that this constructor takes no argument, and it calls the constructor in part d(ii) and pass an empty string as the argument.                                   [ 3 marks ]

c)     Create the Fish class as specified by the UML diagram above. Override the Animal methods to specify that fish cannot walk and do not have legs.
     [ 18 marks ]

Question 2 [ 60 marks ]


Write a program called MyPet that create the Cat and Fish objects and store the objects in a reference-based linked list data structure. You may create a GUI or non-GUI program. Either program will entitle you to a maximum of 15 marks.     [ 15 marks ]

This program allows you to  

a)     create a list of Cat objects, sorted based on its name. Each cat has a unique name.                                                  [ 10 marks ]
b)     create a list of Fish objects, sorted based on its name. Each fish has a unique name too.                                             [ 5 marks ]
c)     display the details of each cat and fish, including what it can do.     [ 10 marks ]
d)     change the name of your pets                              [ 10 marks ]

Additional features such as include the status of the fish, delete a dead cat or dead fish, or other functions.                                        [ 10 marks ]
0
Comment
Question by:elroytiow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 13527585
..it is also in the wrong TA, this is clearly a Java question.

 elroytiow, if you do not do your own home work and learn nothing from your courses will end being unemployable.
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 13527591
>>>.it is also in the wrong TA,

The real wrong TA it is in; is the cross-posted version in Community Support ...LOL

Cd&
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13527955
elroytiow,

We would like to see you come up with some initial coding efforts from your own side before we start helping.
0
 

Author Comment

by:elroytiow
ID: 13531278
Below are my coding

import java.io.*;
import javax.swing.*;
import java.awt.*;

class node{
      private string catname;
      private string fishname;
      private node next;
      name=catname;
      name2=fisname;
public void setcatname(string catname){
      catname=newname;
}

public string getcatname(){
      return catname;
}

public void setfishname(string fishname){
      fishname=newfishname;
}

public string getfishname(){
      return fishname;
}

public void setnext(node next){
      next=node next;
}

public node getnext(){
      return next;
}

public interface interfaceSLList{
      public void add(string catname,string fishname);
      public void delete(string name);
      public void search(string name);
      public void printall();
      public int size();
}

public class Mypet implementing interfaceSLList{
      private node head;
      public Mypet(){
            head=null;
      }

      node node1=new node();

      node1.setcatname("Miao");
      node1.setfishname("Nemo");
}

public int size(){
      if(head==null)
            return 0;
      node.probe==head;
      int count=0;
do{
      count=count +1;
      probe=probe.getnext;
}while(probe != null);

return count;
}}

add newnode(){
      node head=null;
      node newnode=new node();
      newnode.setnode();
      head=newnode;
      newnode.setnext(null);
}

public void printall(){
      if(head==null)
      {system.out.println("The list is empty");
      return;}
      
      node probe=head;
      system.out.println("All item in the list are:");
      do{
            system.out.println("probe.getcatname()+"is cat name"+probe.getfishname()"is fish name");
            probe=probe.getnext();
      }while(probe!=null);
}

public void search(string name){
      if(head==null)
{system.out.println("The list is empty");
}

node.probe=head;
do{
      if(probe.getname().equals(name)){
      system.out.println(name+" is cat name.");
      else
      system.out.println(name2" is fish name.");
      return;
}
probe=probe.getnext();
while(probe!=null);
system.out.println(name&&name2 +"is not in the list");
}
node before=head;
node after=head.getnext();
while(before.getnext()!=null && after.getnext()!=null)
      {
            before=before.getnext();
            after=after.getnext();
}
before.setnext(null);
remove();
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 1000 total points
ID: 13532207
Plenty of errors.

>> public void setcatname(string catname){
>>      catname=newname;
>> }

'string' should be String. In C#, you could've got away with that but Java does not accept 'string' as a data-type. The class-name is String. Moreover, 'newname' is not defined in the above method? You should be using:

this.catname = catname ;

You've made similar mistakes in the other setters too. I hope you can correct them similarly. Also:

>> public void setnext(node next){
>>      next=node next;
>> }

You don't need to write 'node' in the RHS. This should be:

this.next = next ; // the local variable 'next' will over-ride the class-member variable, so you need to resolve using 'this'

>> public class Mypet implementing interfaceSLList{

'implementing' is not a keyword. The right word is 'implements'.

There are couple of errors more. I hope you could fix all of them and post your code if you still face problems (or better upload to a publicly available site and post its URL, if possible, otherwise this Q will become too long with lots of code).
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question