Java Code
import java.awt.*;
import javax.swing.*;
public class CourseApplet extends JApplet
{
Course math, history, cs;
JList list;
DefaultListModel model = new DefaultListModel( );
public void init( )
{
math = new Course( "Calculus", 4 );// call constructor method in Course
history = new Course( "History", 3 );
cs = new Course( "Java", 4 );
list = new JList( model );
model.addElement( math.toString( ) );
model.addElement( history.toString( ) );
model.addElement( cs.toString( ) );
setLayout( new FlowLayout( ) );
add( list );
}
}
public class Course
{
private int numCredits; // instance variable
private String title; // instance variable
public Course( String name, int nCredits ) // constructor
{
title = name;
numCredits = nCredits;
}
public String getTitle( ) // getter method
{
return title;
}
public int getNumCredits( ) // getter method
{
return numCredits;
}
public void setTitle( String t ) // setter method
{
title = t;
}
public void setNumCredits( int nc) // setter method
{
numCredits = nc;
}
public String toString( ) // returnStr is a local variable
{
String returnStr = title;
returnStr += " ( " + numCredits + " ) ";
return returnStr;
}
}
©2006 by E.S.Boese. All Rights Reserved