Chapter 3: Using Classes and Objects (2)
Lab Exercises
Topics Lab
Exercises
Enumerated Types Playing
with Cards (required)
Panels Nested
Panels (required)
Wrapper Classes Experimenting
with the Integer Class (extra credit)
Formatting Classes Formatting
Output (extra credit)
Playing With
Cards
Write a class that defines an enumerated type named Rank with values ace, two, three,
four, five, six, seven, eight, nine, ten, jack, queen, king. The main
method
should do the following:
A blackjack hand: ace and
.....
The faceCard variable should be printed instead of the dots.
A two card
hand: (print card1 and card2)
Hand value: (print the sum of the face values of the two
cards)
Nested Panels
The program NestedPanels.java is from
Listing 3.8 of the text. Save the program to your directory and do the
following:
//********************************************************************
// NestedPanels.java Author: Lewis/Loftus
//
// Demonstrates a basic component hierarchy.
//********************************************************************
import java.awt.*;
import javax.swing.*;
public class NestedPanels
{
//-----------------------------------------------------------------
// Presents two colored panels nested within a third.
//-----------------------------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Nested Panels");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
// Set up first subpanel
JPanel subPanel1 = new JPanel();
subPanel1.setPreferredSize (new Dimension(150, 100));
subPanel1.setBackground (Color.green);
JLabel label1 = new JLabel ("One");
subPanel1.add (label1);
// Set up second subpanel
JPanel subPanel2 = new JPanel();
subPanel2.setPreferredSize (new Dimension(150, 100));
subPanel2.setBackground (Color.red);
JLabel label2 = new JLabel ("Two");
subPanel2.add (label2);
// Set up primary panel
JPanel primary = new JPanel();
primary.setBackground (Color.blue);
primary.add (subPanel1);
primary.add (subPanel2);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
}
Formatting
Output
File Deli.java
contains a partial program that computes the cost of buying an item at the
deli. Save the program to your directory and do the following:
***** CS
Deli *****
Unit Price: $4.25 per pound
Weight: 2.56 pounds
TOTAL: $10.89
// ********************************************************
// DeliFormat.java
//
// Computes the price of a deli item given the weight
// (in ounces) and price per pound -- prints a label,
// nicely formatted, for the item.
//
// ********************************************************
import java.util.Scanner;
public class Deli
{
//
---------------------------------------------------
// main reads in the price per pound of a
deli item
// and number of ounces of a deli item
then computes
// the total price and prints a
"label" for the item
//
--------------------------------------------------
public
static void main (String[] args)
{
final
double OUNCES_PER_POUND = 16.0;
double
pricePerPound;
// price per pound
double
weightOunces;
// weight in ounces
double
weight; // weight in
pounds
double
totalPrice;
// total price for the item
Scanner scan = new Scanner(System.in);
// Declare money as a NumberFormat
object and use the
// getCurrencyInstance method to assign it a value
// Declare fmt as a DecimalFormat object and instantiate
// it to format
numbers with at least one digit to the left of the
// decimal and the
fractional part rounded to two digits.
// prompt the user
and read in each input
System.out.println ("Welcome
to the CS Deli!!\n ");
System.out.print
("Enter the price per pound of your item: ");
pricePerPound = scan.nextDouble();
System.out.print
("Enter the weight (ounces): ");
weightOunces = scan.nextDouble();
// Convert ounces to
pounds and compute the total price
weight = weightOunces / OUNCES_PER_POUND;
totalPrice = pricePerPound
* weight;
// Print the label
using the formatting objects
// fmt for the weight in pounds and money for the prices
}
}
Experimenting
with the Integer Class
Wrapper classes are described on pages 138-140 of the text. They
are Java classes that allow a value of a primitive type to be "wrapped
up" into an object, which is sometimes a useful thing to do. They also
often provide useful methods for manipulating the associated type. Wrapper
classes exist for each of the primitive types: boolean, char, float, double, int,
long, short, and byte.
Write a program IntWrapper that uses the
constants and methods of the Integer class (page 140 for a short list, pages
819-820 for a complete list) to perform the following tasks. Be sure to clearly
label your output and test your code for each task before proceding.