Final Exam Notes - Intermediate Java Programming | COP 3804, Exams of Java Programming

Material Type: Exam; Class: Intermediate Java Programming; Subject: Computer Programming; University: Florida International University; Term: Summer 2007;

Typology: Exams

Pre 2010

Uploaded on 03/11/2009

koofers-user-bj9
koofers-user-bj9 🇺🇸

5

(1)

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
COP-3804 Intermediate Java Programming Final Date: Aug, 7th 2007 Grade: _____/100
(This final consists of three pages)
Name: _______________________________________________ Last 4 digits PantherID: __________
We are given the following classes: an abstract class Animal, an interface Whistler, another interface
Fyable and a concrete class Parrot that inherits from class Animal and implements interfaces Whistler and
Flyable. Complete implementations for these classes and interfaces are provided below.
Class Animal has two abstract methods eat() and
whoAmI(), that concrete (non-abstract) classes need to
implement.
public abstract class Animal{
private String id;
public Animal( ){
id = "";
}
public Animal( String id ){
this.id = id;
}
public void setId( String id ){
this.id = id;
}
public String getId( ){
return id;
}
public abstract void eat( );
public abstract void whoAmI( );
}
Interface Whistler has one method to be implemented
by classes inheriting from it.
public interface Whistler{
int LOUD = 10;
int LOW = 0;
void whistle( );
}
Interface Flyable allows a class to implement the
flying behavior; that class needs to implement: fly,
canFly and isAbleToFly methods.
public interface Flyable{
void fly( );
void canFly( boolean can );
boolean isAbleToFly( );
}
Class Parrot implements abstract methods eat() and
whoAmI() from class Animal, and also method
whistle() from interface Whistler (see next column).
public class Parrot extends Animal
implements Whistler, Flyable {
private int intensity;
private boolean flies;
public Parrot( ){
intensity = LOW;
flies = true;
}
public Parrot( int intensity, String id ){
super(id);
this.intensity = intensity;
flies = true;
}
public Parrot( boolean flies, int intensity, String id ){
super(id);
this.intensity = intensity;
this.flies = flies;
}
public void setIntensity( int intensity ){
this.intensity = intensity;
}
public int getIntensity( ){
return intensity;
}
public void eat( ){
System.out.println( "Parrot "+getId()+" eating." );
}
public void whoAmI( ){
System.out.println( "I'm parrot "+getId()+"." );
}
public void whistle( ){
String intStr = "loudly";
if ( intensity < LOUD )
intStr = "lowly";
System.out.println( "Parrot "+getId()+
" whistling "+intStr+"." );
}
public void fly( ){
if ( isAbleToFly() )
System.out.println( "Parrot "+getId()+" is flying." );
else
System.out.println( "Parrot "+getId()+
" wished it could fly." );
}
public void canFly( boolean can ){
flies = can;
}
public boolean isAbleToFly( ){
return flies;
}
}
pf3

Partial preview of the text

Download Final Exam Notes - Intermediate Java Programming | COP 3804 and more Exams Java Programming in PDF only on Docsity!

COP-3804 Intermediate Java Programming Final Date: Aug, 7

th

2007 Grade: _____/

(This final consists of three pages)

Name: _______________________________________________ Last 4 digits PantherID: __________

We are given the following classes: an abstract class Animal , an interface Whistler , another interface

Fyable and a concrete class Parrot that inherits from class Animal and implements interfaces Whistler and

Flyable. Complete implementations for these classes and interfaces are provided below.

Class Animal has two abstract methods eat() and

whoAmI() , that concrete (non-abstract) classes need to

implement.

public abstract class Animal { private String id;

public Animal ( ){ id = ""; }

public Animal ( String id ){ this.id = id; }

public void setId ( String id ){ this.id = id; }

public String getId ( ){ return id; }

public abstract void eat ( );

public abstract void whoAmI ( ); }

Interface Whistler has one method to be implemented

by classes inheriting from it.

public interface Whistler { int LOUD = 10; int LOW = 0;

void whistle ( ); }

Interface Flyable allows a class to implement the

flying behavior; that class needs to implement: fly ,

canFly and isAbleToFly methods.

public interface Flyable {

void fly ( );

void canFly ( boolean can );

boolean isAbleToFly ( ); }

Class Parrot implements abstract methods eat() and

whoAmI() from class Animal , and also method

whistle() from interface Whistler (see next column).

public class Parrot extends Animal implements Whistler , Flyable { private int intensity; private boolean flies; public Parrot ( ){ intensity = LOW; flies = true; } public Parrot ( int intensity, String id ){ super(id); this.intensity = intensity; flies = true; } public Parrot ( boolean flies, int intensity, String id ){ super(id); this.intensity = intensity; this.flies = flies; } public void setIntensity ( int intensity ){ this.intensity = intensity; } public int getIntensity ( ){ return intensity; } public void eat ( ){ System.out.println( "Parrot " +getId()+ " eating." ); } public void whoAmI ( ){ System.out.println( "I'm parrot " +getId()+ "." ); } public void whistle ( ){ String intStr = "loudly" ; if ( intensity < LOUD ) intStr = "lowly" ; System.out.println( "Parrot " +getId()+ " whistling " +intStr+ "." ); } public void fly ( ){ if ( isAbleToFly() ) System.out.println( "Parrot " +getId()+ " is flying." ); else System.out.println( "Parrot " +getId()+ " wished it could fly." ); } public void canFly ( boolean can ){ flies = can; } public boolean isAbleToFly ( ){ return flies; } }

1. (5 pts) Add methods toString and equals to classes Animal and Parrot. Method toString returns a string

with information on the object, and method equals compares the object with another object of the same

class returning true or false depending whether the two objects fields have the same values.

2. (20 pts) Create a class Human that derives from class Animal and implements interface Whistler. Class

Human has two attributes, an integer intensity (for the whistle intensity) and a string name. You need to

provide the proper constructors as well as the accessor and mutator methods for those fields.

3. (5 pts) Given the following NameException class:

public class NameException extends Exception {

public NameException(String msg){

super(msg);

When constructing an object of class Human , the overloaded constructor methods need to raise an

exception whenever the name as parameter is the empty string or null (whenever there is an attempt at

constructing a Human with an empty name).

4. (5 pts) Given the previous modified implementation using exceptions, how would you modify the main

function of class Final to compile correctly? (there are checked exceptions to be taken care of):

public class Final {

public static void main (String[] args){

Human m1 = new Human();

Human m2 = new Human(30,"Nicholas Negroponte");

Human m3 = new Human(30,"");

5. (20 pts) Implement a linked list AnimalList (don’t forget the AnimalNode class) that allows performing

the following operations:

AnimalList list = new AnimalList ();

list.add( new Parrot (5,"miky") );

try{

list.add( new Human (1,"Joseph","ID1") );

} catch( NameException e ){

e.printStackTrace();

System.out.println( list.toString() );

The last statement should print:

Parrot miky

Joseph

on the screen.

6. (15 pts) There is a text file that contains the information on different animals. This is an excerpt of one

of those files:

Human,10,John Smith,ID

Parrot,60,Miky,true