CS111 Lab 2:
OOP Basics
Create a Java class that represents a date.
A date is defned by month, day, and year. Each of these attributes are private data members.
Include a default and explicit constructor.
Include setters and getters.
In addition, allow functionality that formats the date as mm/dd/yyyy. This method will be called toString().
Test your class by building an array containing 10 dates. Display all the dates before the year 2000.
Construct a class Time that represents a moment in time, such as 8:30AM or 5:22PM
- What are the attributes that characterize a Time object?
- What are the operations that allow access to these attributes?
- Write a Time Java class.
- Write a main() method to test the complete class and its methods.
Exercise 3: Testscore Class
|
Construct a class Testscore that represents a score on an exam.
- What are the attributes that characterize a Testscore object? Consider that a test score
is a value between 0 and 100 and that it also has a letter grade assigned to it. For example,
a test score of 100 is also an A and a test score of 88 is also a B+.
- What are the operations that allow access to these attributes?
- Write a Testscore Java class.
- Write a main() method to test the complete class and its methods.
Create a Java class that represents a card in a deck of cards.
Card.java
public class Card {
//--------------------------------------------------------------------
//PART I: DATA MEMBERS
//--------------------------------------------------------------------
private int rank;
private int suit;
private boolean isFaceUp;
//--------------------------------------------------------------------
//PART II: Explicit Constructor
//--------------------------------------------------------------------
public Card(int r, int s) {
rank = r;
suit = s;
isFaceUp = false;
}
//--------------------------------------------------------------------
//PART III: NO TRADITIONAL SETTERS AND GETTERS
//--------------------------------------------------------------------
//--------------------------------------------------------------------
//PART IV: FUNCTIONALITY
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Returns the rank value of this card.
public int value() {
if (rank >= 10)
return 10;
else
return rank;
}
//--------------------------------------------------------------------
// Flips the card.
public void flip() {
isFaceUp = !isFaceUp;
}
//--------------------------------------------------------------------
// Returns a true if the card is face up, false otherwise.
public boolean faceUp() {
return isFaceUp;
}
//--------------------------------------------------------------------
//Returns the String value of the rank and suit of this card.
public String toString() {
String value = "";
switch (rank) {
case 1: value += "ACE"; break;
case 2: value += "TWO"; break;
case 3: value += "THREE"; break;
case 4: value += "FOUR"; break;
case 5: value += "FIVE"; break;
case 6: value += "SIX"; break;
case 7: value += "SEVEN"; break;
case 8: value += "EIGHT"; break;
case 9: value += "NINE"; break;
case 10: value += "TEN"; break;
case 11: value += "JACK"; break;
case 12: value += "QUEEN"; break;
case 13: value += "KING";
}
switch (suit) {
case 1: value += " of SPADES"; break;
case 2: value += " of HEARTS"; break;
case 3: value += " of DIAMONDS"; break;
case 4: value += " of CLUBS";
}
return value;
}
}
Create a StopWatch class in Java. A StopWatch object can be used to compute the time it takes
to perform an algorithmic action. What are the data elements needed for this class?
What are the setters and getters and functionality? To access the system time, use System.currentTimeMillis().
Test this StopWatch by using it to time a for loop outputing all the odd numbers from 1 up to 10 million.