Right click on each of these:
and choose Save Target As... (or Save Link As...) and save the files in the src folder of your project.
Student
objects to represent data describing 15 different students.
The output is printed in a JTextArea
GUI
component using several append method calls. MUPanel
class, change the first element in the array (element 0) so
that it represents you. Change "Samantha Student" to your
name, change the dorm to your residence hall (or use "off-campus" if
you don't live in a residence hall), specify a code number that
indicates your current class (1=Freshman, 2=Sophomore,
3=Junior, 4=Senior), specify a Boolean
value
indicating whether or not you are an intercollegiate athlete, and put in a
decimal number representing your GPA (you may use your
actual GPA or a made-up value, as you wish).Student
objects. Then add
instantiations for a sixteenth and a seventeenth student in
the fillArray()
method that represents either
friends of yours, or made-up people (could be your favorite
historical figures, athletes, movie stars, etc.). displayEntireArray()
method, then
paste a copy of this code immediately after the method, and then change the name of the
newly copied method to displaySophomores
.displaySophomores
method so that it only
processes students in the array that are Sophomores (have a
classCode
value of 2). To do this, place an if
statement immediately inside the for
loop, and
test to see if the result of the getClassCode()
method called for element number k
of the studentRoster
array is equal to 2. Only if this test evaluates as true
should the student in position k
of the array
be printed and her gpa added to the
total. Also, edit the text strings in this second block so
that the first line printed by the block is "Listing of
Sophomores".MUPanel
constructor method
to invoke this method; it should be invoked AFTER displayEntireArray
has
been invoked. Run the program now; you should see
two sets of output lines in the JTextArea: the original list of
all students, and a second list showing only those students
who are Sophomores.String
for equality, use .equals("McMaster Hall")
-- don't use
the ==
operator for the test).MUPanel
constructor method to invoke these methods.Right click on MUPanel.java and choose Save Target As... (or Save Link As...) to save the file in the src folder of your project.
MUPanel
,
displayEntireArray
and fillArray
try
to use an array of Book
objects, and the Book
class definition is not supplied. Your task for this step of
the lab is to provide a complete class definition for a Book
class, that allows the program to run correctly. We will also learn about
the "Insert Code" feature of NetBeans that will do a lot of the work of
writing a new class definition for us.Book
class must have three (3) private
data members: a String
for the book's title, a
String
for the book's author, and an Integer
for the number of pages in the book. We also will need a constructor method
for the class, a getNumPages()
method
that returns the number of pages in a Book
object, three setter methods (one for each private data member of the Book
class), and a toString()
method.String
s for the title and for the author of the book, and an Integer for the number of pages of the book (use the names title
, author
and numPages
for these three properties.toString
method. This is such a basic task that NetBeans includes code generation features to do this automatically for a class once we have declared the properties that the class will contain. We will use this code completion feature to save ourselves some time and work in getting the Book
class to be operational.getNumPages
method; so that means that we should only check one checkbox in the dialog that appears, beside the numPages
entry, and then when we click the Generate button only one getter is added to the class definition.Book
class definition so we can run
the starter program for Lab 9b — a toString
method. NetBeans will generate a very generic toString for us, and then we can edit the method so that the output is exactly the way we want it. Choose "Source | Insert Code" now, then choose "toString..." from the pop-up menu. Be sure the checkboxes for all of the fields of the class are checked, and click "Generate". Notice that the code that NetBeans writes displays the name of the class, and then the name of each field followed by the value of the field. Let's see what the result of this toString
method looks like....Book
class, you should see the following output:toString
method in the Book
class.toString
method so that the value returned has the following format:
title_of_book, by author_of_book, has number_of_pages pages.When you have correctly re-written the
toString
method, you should obtain output that looks like this (don't forget to put a period at the end):Book
class: an Integer
value representing the copyright
year for the book, and a Boolean
value
representing whether the book is a hardback book or not. Now we will
need a different constructor method for the class that accepts parameters for all
five of the properties the class now contains. In the Book
class,
delete the current constructor method, then use "Source | Insert Code" to insert a
new constructor that instantiates all five properties of the class.
toString()
method for the Book
class should be edited as follows: first of all, replace the single return
statement in the method body with this code:
String answer = title + ", by " + author + ", has " + numPages + " pages."; return answer;This would do the same thing as the original code, but now we have a variable named
answer
that we can use in an if/else statement, which we will do right now. At the end of the first line above, concatenate
the string " (c) ", followed by the copyright year for the
book, followed by the string "; ". Then, using an if/else
statement, concatenate the string "Hardback" to the answer
String if the hardback
private data member is true, or the string "Paperback" if
the hardback
private data member is false.MUPanel
class (as well as
displayArray
and fillEntireArray)
will
also need some modifications. For each book, you'll need to
specify two more pieces of information when the book is
instantiated. Use these values:Title | Copyright Year | Hardback? |
---|---|---|
War and Peace | 1864 | true |
Gone With The Wind | 1936 | true |
Using Java | 2011 | false |
Autobiography of a Yogi | 1953 | false |
Computing Unbound | 1992 | false |
Heaven Help Us | 1996 | false |
Computer Systems | 2010 | true |
Book
class definition and MUPanel
, you should
obtain output that looks like this when you run the program:append()
method calls in displayEntireArray
and add if
statements to the for
loop so that the program calculates the totals and produces
the output shown in this final screen shot: