Tutorial08 - public class CardGame { public static void...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Tutorial #8: Classes O P T I O N A L Introduction: This tutorial is recommended if you have limited experience writing template classes. It uses code presented in the online quiz for this material. 1. Create a file called PlayingCard.java with the following code. This is a template class. Note that it has one instance variable and two methods, one of which is a constructor. Compile this code when you are done. public class PlayingCard { int value; PlayingCard(int newValue) { value = newValue; } int getValue() { return value; } } 2. Create a file called CardGame.java with the following code. This is an application class. To keep things simple, we will “hard code” some values instead of using random numbers.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: public class CardGame { public static void main(String args) { PlayingCard card1, card2; card1 = new PlayingCard(10); card2 = new PlayingCard(3); System.out.println("Card 1 has a value of " + card1.getValue()); System.out.println("Card 2 has a value of " + card2.getValue()); } } In this example, we create two instances of the PlayingCard class. After declaring the class instances, we initialize them to the values 10 and 3 using the constructor method. Then we print the values using the getValue() method. 3. Compile CardGame.java and run it. You should see the following output: Card 1 has a value of 10 Card 2 has a value of 3...
View Full Document

This test prep was uploaded on 04/10/2008 for the course ENGR 131 taught by Professor Xiaowei during the Spring '07 term at Case Western.

Ask a homework question - tutors are online