Description
Lab 8: De fining Classes and Methods
static & Constructor Edition
Abstract
Your task is to complete two problems from the textbook.
Create a class RoomOccupancy that can be used to record the number of people in the rooms of a building. The class has the attributes:
numberInRoom { the number of people in a room
totalNumber { the total number of people in all rooms as a static
variable The class has the following methods:
addOneToRoom{ adds a person to the room and increases the value of totalNumber
removeOneFromRoom { removes a person from the room, ensuring that numberInRoom does not go below zero, and decreases the value of totalNumber as needed
getNumber {returns the number of people in the room
getTotal {a static method that returns the total number of people
Create a few RoomOccupancy classes and con rm they work.
2 Assignment 2
Programming Project 10 from the textbook.
Write a program that will record the votes for one of two candidates by using the class VoteRecorder, which you will design and create. VoteRecorder will have static variables to keep track of the total votes for candidates and
instance variables to keep track of the votes made by a single person.
It will have the following attributes:
nameCandidatePresident1 { a static string that holds the name of the rst candidate for president
nameCandidatePresident2 { a static string that holds the name of the second candidate for president
nameCandidateVicePresident1 { a static string that holds the name of the rst candidate for vice president
nameCandidateVicePresident2 { a static string that holds the name of the second candidate for vice president
votesCandidatePresident1 { a static integer that holds the number of votes for the rst candidate for president
votesCandidatePresident2 { a static integer that holds the number of votes for the second candidate for president
votesCandidateVicePresident1 { a static integer that holds the number of votes for the rst candidate for vice president
votesCandidateVicePresident2 { a static integer that holds the number of votes for the second candidate for vice president
myVoteForPresident { an integer that holds the vote of a single in- dividual for president (0 for no choice, 1 for the rst candidate, and 2 for the second candidate)
myVoteForVicePresident { an integer that holds the vote of a single individual for vice president (0 for no choice, 1 for the rst candidate, and 2 for the second candidate)
Continued on the next page
In addition to appropriate constructors, VoteRecorder has the following methods:
setCandidatesPresident(String name1, String name2) { a static method that sets the names of the two candidates for president
setCandidatesVicePresident(String name1, String name2) { astatic method that sets the names of the two candidates for vice president
resetVotes {a static method that resets the vote counts to zero
getCurrentVotePresident {a static method that returns a string with the current total number of votes for both presidential candidates
getCurrentVoteVicePresident { a static method that returns a string with the current total number of votes for both vice presidential candidates
getAndConfirmVotes { a non-static method that gets an individuals votes, con rms them, and then records them
getAVote(String name1, String name2) { a private method that returns a vote choice for a single race from an individual (0 for no choice, 1 for the rst candidate, and 2 for the second candidate)
getVotes { a private method that returns a vote choice for president and vice president from an individual
confirmVotes { a private method that displays a persons vote for president and vice president, asks whether the voter is happy with these choices, and returns true or false according to a yes-or-no response
recordVotes { a private method that will add an individuals votes to the appropriate static variables