![]() |
![]() |
|
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
I will give examples of objects using
class Song {
String title;
String artist;
double length; // in seconds.
boolean isCopyrighted;
}
|
In the registrar's computer system, a student has a name, a sid, a gpa, a major, and a number of credit hours completed.
Your task: Make a class to represent a student.
Constructors are just functions:
You pass them arguments, and they return a value — in particular,
they return a newly-constructed object (a
Java forces you to write constructors a bit differently than most functions, though:
class Song extends Object120 {
String title;
String artist;
double length; // in seconds.
boolean isCopyrighted;
// define the constructor:
Song( String t, String a, double l, boolean c ) {
//some voodoo (for now):
super(t,a,l,c);
}
static void testSongs() {
Song s;
s = new Song( "Help!", "The Beatles", 122, true );
}
}
|
Scenario: In the registrar's computer system, a student has a name, a sid, a gpa, a major, and a number of credit hours completed.
Create a
What is the signature for the
Write the constructor;
for its body use the mysterious call to
(On Friday we'll see how to replace that call to
Call the constructor,
to create an example of a
Inspect the result in BlueJ, by clicking on the small red return value (in code pad),
or the Inspect button if you called the constructor interactively.
As usual, make a function
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
| ©2012, Ian Barland, Radford University Last modified 2012.Oct.01 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarland |
![]() |