0% found this document useful (0 votes)
87 views14 pages

Ch. 7 Programs

The document defines several classes for different types of reading materials like books, novels, magazines and textbooks. It creates objects of these classes and calls their methods to output title, author, ISBN and other details. It also has a main class that creates an array of reading material objects and calls their content method.

Uploaded by

iameunwoobae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views14 pages

Ch. 7 Programs

The document defines several classes for different types of reading materials like books, novels, magazines and textbooks. It creates objects of these classes and calls their methods to output title, author, ISBN and other details. It also has a main class that creates an array of reading material objects and calls their content method.

Uploaded by

iameunwoobae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

//Grace Bae

//3/9/15
//Block 3
//7.1.1
public class Coin
{
public final int HEADS = 0;
public final int TAILS = 1;
private int face;
public Coin ()
{
flip();
}
public void flip ()
{
face = (int) (Math.random() * 2);
}
public int getFace ()
{
return face;
}
public String toString()
{
String faceName;
if (face == HEADS)
faceName = "Heads";
else
faceName = "Tails";
}

return faceName;

}
//Grace Bae
//3/9/15
//Block 3
//7.1.2
public class MonetaryCoin extends Coin
{
private int value;
public MonetaryCoin (int money)
{
super();
value = money;
}

public void setValue (int money)


{
value = money;
}
public int getValue ()
{
return value;
}
public String toString()
{
String result = super.toString();
result += "\t" + value;
return result;
}

}
//Grace Bae
//3/9/15
//Block 3
//7.1.3

public class MonetaryCoinDriver


{
public static void main (String[] args)
{
MonetaryCoin[] coins = new MonetaryCoin[7];
coins[0]
coins[1]
coins[2]
coins[3]
coins[4]
coins[5]
coins[6]

=
=
=
=
=
=
=

new
new
new
new
new
new
new

MonetaryCoin(1);
MonetaryCoin(5);
MonetaryCoin(10);
MonetaryCoin(25);
MonetaryCoin(50);
MonetaryCoin(100);
MonetaryCoin(100);

for (int flipping = 0; flipping < coins.length; flipping++)


coins[flipping].flip();
int sum = 0;
for (int adding = 0; adding < coins.length; adding++)
sum += coins[adding].getValue();
for (int index = 0; index < coins.length; index++)
System.out.println (coins[index]);
}

System.out.println ("\nTotal Value: " + sum);

}
----jGRASP exec: java MonetaryCoinDriver

Heads
1

Heads
5
Tails
10
Tails
25
Tails
50
Tails
100
Tails
100

Total Value: 291

----jGRASP: operation complete.


//Grace Bae
//3/9/15
//Bock 3
//7.2.1
public class Hospital
{
public static void main (String [] args)
{
HospitalEmployee vito = new HospitalEmployee ("Vito", 123);
Doctor michael = new Doctor ("Michael", 234, "Heart");
Surgeon vincent = new Surgeon ("Vincent", 645, "Brain", true);
Nurse sonny = new Nurse ("Sonny", 789, 6);
Administrator luca = new Administrator ("Luca", 375, "Business");
Receptionist tom = new Receptionist ("Tom", 951, "Talking", true);
Janitor anthony = new Janitor ("Anthony", 123, "Maintenence", false);
System.out.println
System.out.println
System.out.println
System.out.println
System.out.println
System.out.println
System.out.println

(vito);
(michael);
(vincent);
(sonny);
(luca);
(tom);
(anthony);

vito.work();
michael.diagnose();
vincent.operate();
sonny.assist();
luca.administrate();
tom.answer();
anthony.sweep();
}
}
//Grace Bae
//3/9/15
//block 3
//7.2.2
public class HospitalEmployee
{
protected String name;
protected int number;

public HospitalEmployee (String empName, int empNumber)


{
name = empName;
number = empNumber;
}
public void setName (String empName)
{
name = empName;
}
public void setNumber (int empNumber)
{
number = empNumber;
}
public String getName()
{
return name;
}
public int getNumber()
{
return number;
}
public String toString()
{
return name + "\t" + number;
}
public void work()
{
System.out.println (name + " works for the hospital.");
}

}
//Grace Bae
//3/9/15
//Block 3
//7.2.3

public class Doctor extends HospitalEmployee


{
protected String specialty;
public Doctor (String empName, int empNumber, String special)
{
super (empName, empNumber);
specialty = special;
}

public void setSpecialty (String special)


{
specialty = special;
}
public String getSpecialty()
{
return specialty;
}
public String toString()
{
return super.toString() + "\t" + specialty;
}
public void diagnose()
{
System.out.println (name + " is a(n) " + specialty + " doctor.");
}

}
//Grace Bae
//3/9/15
//Block 3
//7.2.4

public class Surgeon extends Doctor


{
protected boolean operating;
public Surgeon (String empName, int empNumber,
String special, boolean isOper)
{
super (empName, empNumber, special);
operating = isOper;
}
public void setIsOperating (boolean isOper)
{
operating = isOper;
}
public boolean getIsOperating()
{
return operating;
}
public String toString()
{
return super.toString() + "\tOperating: " + operating;
}
public void operate()
{

System.out.print (name + " is");


if (!operating)
System.out.print (" not");
System.out.println (" operating now.");
}
}
//Grace Bae
//3/9/15
//Block 3
//7.2.5
public class Nurse extends HospitalEmployee
{
protected int numPatients;
public Nurse (String empName, int empNumber, int numPat)
{
super (empName, empNumber);
numPatients = numPat;
}
public void setNumPatients (int pat)
{
numPatients = pat;
}
public int getNumPatients()
{
return numPatients;

public String toString()


{
return super.toString() + " has " + numPatients + " patients.";
}
public void assist()
{
System.out.println (name + " is a nurse with " +
numPatients + " patients.");
}
}
//Grace Bae
//3/9/15
//Block 3
//7.2.6
public class Administrator extends HospitalEmployee
{
protected String department;
public Administrator (String empName, int empNumber, String dept)
{

super (empName, empNumber);


department = dept;
}
public void setDepartment (String dept)
{
department = dept;
}
public String getDepartment()
{
return department;
}
public String toString()
{
return super.toString() + " works in " + department;
}
public void administrate()
{
System.out.println (name + " works in the " +
department + " department.");
}
}
//Grace Bae
//3/9/15
//Block 3
//7.2.7
public class Janitor extends Administrator
{
protected boolean sweeping;
public Janitor (String ename, int enum1, String dept, boolean sw)
{
super (ename, enum1, dept);
sweeping = sw;
}
public void setIsSweeping (boolean isS)
{
sweeping = isS;
}
public boolean getIsSweeping ()
{
return sweeping;
}
public String toString ()
{

return super.toString() + "\tSweeping: " + sweeping;

public void sweep()


{
System.out.print (name + " is");
if (!sweeping)
System.out.print (" not");
System.out.println (" sweeping the floor.");
}

}
//Grace Bae
//3/9/15
//Block 3
//7.2.8

public class Receptionist extends Administrator


{
protected boolean answering;
public Receptionist (String ename, int enum1, String dept,
boolean ans)
{
super (ename, enum1, dept);
answering = ans;
}
public void setIsAnswering (boolean isA)
{
answering = isA;
}
public boolean getIsAnswering()
{
return answering;
}
public String toString()
{
return super.toString() + "\tAnswering: " + answering;
}
public void answer()
{
System.out.print (name + " is");
if (!answering)
System.out.print (" not");
System.out.println (" answering the phone.");
}
}
----jGRASP exec: java Hospital

Vito
123
Michael 234
Heart
Vincent 645
Brain
Operating: true
Sonny
789 has 6 patients.
Luca
375 works in Business
Tom
951 works in Talking
Answering: true
Anthony 123 works in Maintenence
Sweeping: false
Vito works for the hospital.
Michael is a(n) Heart doctor.
Vincent is operating now.
Sonny is a nurse with 6 patients.
Luca works in the Business department.
Tom is answering the phone.
Anthony is not sweeping the floor.

----jGRASP: operation complete.


//Grace Bae
//3/9/15
//Block 3
//7.3.1
public class BookClub
{
public static void main (String[] args)
{
ReadingMatter[] rm = new ReadingMatter[5];
rm[0] = new ReadingMatter ("Myst Strategy", "0-7615-0807-4");
rm[1] = new Book ("Great Eskimo Vocabulary Hoax, The",
"0-226-68534-9",
"Pullum, Geoffrey");
rm[2] = new TextBook ("Java Software Solutions",
"0-201-61271-2",
"Lewis, John and William Loftus",
true);
String[] names = { "Hazel", "Fiver", "Bigwig",
"Blackberry", "Dandelion" };
rm[3] = new Novel ("Watership Down",
"0-380-00293-0",
"Adams, Richard", names);
rm[4] = new Magazine ("ACM Crossroads",
"0-234-5678-0",
"Perry, Lynellen and others");
for (int index = 0; index < rm.length; index++)
{
rm[index].content();
System.out.println();
}

}
}
//Grace Bae
//3/9/15
//Block 3
//7.3.2
public class ReadingMatter
{
protected String title, isbn;
public ReadingMatter (String thisTitle, String isbnNum)
{
title = thisTitle;
isbn = isbnNum;
}
public void setTitle (String thisTitle)
{
title = thisTitle;
}
public void setISBN (String isbnNum)
{
isbn = isbnNum;
}
public String getTitle()
{
return title;
}
public String getISBN()
{
return isbn;
}
public String toString()
{
return (title + "\t" + isbn);
}
public void content()
{
System.out.println ("Title: " + title);
System.out.println ("ISBN: " + isbn);
}
}
//Grace Bae
//3/9/15
//Block 3
//7.3.3
public class Book extends ReadingMatter

protected String author;


public Book (String thisTitle, String isbnNum, String auth)
{
super (thisTitle, isbnNum);
author = auth;
}
public void setAuthor (String auth)
{
author = auth;
}
public String getAuthor()
{
return author;
}
public String toString()
{
return super.toString() + "\t" + author;
}
public void content()
{
super.content();
System.out.println ("Author: " + author);
}

}
//Grace Bae
//3/9/15
//Block 3
//7.3.4

public class Novel extends Book


{
protected String[] characters;
public Novel (String thisTitle, String isbnNum, String auth,
String[] chars)
{
super (thisTitle, isbnNum, auth);
characters = chars;
}
public void setCharacters (String[] chars)
{
characters = chars;
}
public String[] getCharacters()

return characters;

}
public String toString()

String result = super.toString();


for (int index = 0; index < characters.length; index++)
result += "\n" + characters[index];
return result;
}
public void content()
{
super.content();
for (int index = 0; index < characters.length; index++)
System.out.println (characters[index]);
}
}
//Grace Bae
//3/9/15
//Block 3
//7.3.5
public class TextBook extends Book
{
protected boolean answers;
public TextBook (String thisTitle, String isbnNum, String auth,
boolean ans)
{
super (thisTitle, isbnNum, auth);
answers = ans;
}
public void setAnswers (boolean ans)
{
answers = ans;
}
public boolean getAnswers()
{
return answers;
}
public String toString ()
{
String result = super.toString();
if (!answers)
result += "no ";
result += "answers given";
return result;
}

public void content()


{
super.content();
System.out.println ("Answers provided: " + answers);
}

//Grace Bae
//3/9/15
//Block 3
//7.3.6
public class Magazine extends ReadingMatter
{
protected String editor;
public Magazine (String thisTitle, String isbnNum, String ed)
{
super (thisTitle, isbnNum);
editor = ed;
}
public void setEditor (String ed)
{
editor = ed;
}
public String getEditor()
{
return editor;
}
public String toString()
{
String result = super.toString();
result += "\t" + editor;
return result;
}
public void content()
{
super.content();
System.out.println ("Editor: " + editor);
}
}
----jGRASP exec: java BookClub

Title: Myst Strategy


ISBN: 0-7615-0807-4

Title: Great Eskimo Vocabulary Hoax, The


ISBN: 0-226-68534-9

Author: Pullum, Geoffrey

Title: Java Software Solutions


ISBN: 0-201-61271-2
Author: Lewis, John and William Loftus
Answers provided: true

Title: Watership Down


ISBN: 0-380-00293-0
Author: Adams, Richard
Hazel
Fiver
Bigwig
Blackberry
Dandelion

Title: ACM Crossroads


ISBN: 0-234-5678-0
Editor: Perry, Lynellen and others

----jGRASP: operation complete.

You might also like