Sun Microsystems

Wake Tech

Your Guide to Java

Samples
Syllabus
Assignments
Instructors
Introduction
Samples Index
 

CSC 148 Java Programming
Sample Solutions
Chapter Two

// Exercise 2.11 Solution

// Gas.java

// Program calculates average mpg

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

 

public class Gas extends Applet implements ActionListener {

TextField milesInput;

TextField gallonsInput;

Label milesLabel;

Label gallonsLabel;

int miles, gallons;

int totalMiles, totalGallons;

float milesPerGallon;

float totalMilesPerGallon;

 

public void init()

{

milesLabel = new Label( "Enter Miles: " );

gallonsLabel = new Label( "Enter Gallons: " );

milesInput = new TextField( 5 );

gallonsInput = new TextField( 5 );

gallonsInput.addActionListener( this );

add( milesLabel );

add( milesInput );

add( gallonsLabel );

add( gallonsInput );

}

 

public void paint( Graphics g )

{

g.drawString( "MPG this tankful: " + milesPerGallon, 25, 70 );

g.drawString( "Total MPG: " + totalMilesPerGallon, 25, 85 );

}

public void actionPerformed( ActionEvent e )

{

miles = Integer.parseInt( milesInput.getText() );

totalMiles += miles;

gallons = Integer.parseInt( gallonsInput.getText() );

totalGallons += gallons;

if ( gallons != 0 )

milesPerGallon = ( float ) miles / gallons;

 

if ( totalGallons != 0 )

totalMilesPerGallon = ( float ) totalMiles / totalGallons;

 

repaint();

}

}

 

// Exercise 2.12 Solution

// Credit.java

// Program monitors accounts

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

 

public class Credit extends Applet

implements ActionListener {

TextField accountInput, oldBalanceInput,

chargeInput, creditInput,

limitInput;

int account, newBalance,

oldBalance, credits, creditLimit, charges;

boolean exceeded;

Label accountLabel, oldLabel,

chargeLabel, creditLabel, limitLabel;

 

public void init()

{

accountLabel = new Label( "Enter Account:" );

oldLabel = new Label( "Enter Balance: " );

chargeLabel = new Label( "Enter Charges: " );

creditLabel = new Label( "Enter Credits: " );

limitLabel = new Label( "Enter Credit Limit: " );

 

accountInput = new TextField( 5 );

oldBalanceInput = new TextField( 5 );

chargeInput = new TextField( 5 );

creditInput = new TextField( 5 );

limitInput = new TextField( 5 );

limitInput.addActionListener( this );

 

add( accountLabel );

add( accountInput );

add( oldLabel );

add( oldBalanceInput );

add( chargeLabel );

add( chargeInput );

add( creditLabel );

add( creditInput );

add( limitLabel );

add( limitInput );

}

public void actionPerformed( ActionEvent e )

{

exceeded = false;

 

account = Integer.parseInt( accountInput.getText() );

oldBalance = Integer.parseInt( oldBalanceInput.getText() );

charges = Integer.parseInt( chargeInput.getText() );

credits = Integer.parseInt( creditInput.getText() );

creditLimit = Integer.parseInt( limitInput.getText() );

 

newBalance = oldBalance + charges - credits;

showStatus( "New balance is " + newBalance );

 

if ( newBalance > creditLimit )

exceeded = true;

repaint();

}

 

public void paint( Graphics g )

{

if ( exceeded == true )

g.drawString( " CREDIT LIMIT EXCEEDED. ", 2, 149 );

}

}

 

// Exercise 2.13 Solution

// Sales.java

// Program calculates sales

import java.io.IOException;

 

public class Sales {

public static void main( String args[] ) throws IOException

{

double gross = 0.0, net;

int product;

 

System.out.println( "Enter the product sold ( A-D )."

+ " X to quit" );

 

product = System.in.read();

 

while ( product != 'X' ) {

 

if ( product == 'A' )

gross = gross + 239.99;

else if ( product == 'B' )

gross = gross + 129.75;

else if ( product == 'C' )

gross = gross + 99.95;

else if ( product == 'D' )

gross = gross + 350.89;

System.in.skip( 2 );

System.out.println( "Enter the product sold ( A-D )."

+ " X to quit" );

 

product = System.in.read();

}

 

net = 0.09 * gross + 200;

System.out.println( "Earnings are $" + net );

}

}

 

// Exercise 2.14 Solution

// Wages.java

// Program calculates wages

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

 

public class Wages extends Applet

implements ActionListener {

double pay;

TextField rateInput, hourInput;

Label rateLabel, hourLabel;

int hours, rate;

 

public void init()

{

rateInput = new TextField( 5 );

rateLabel = new Label( "Enter hourly rate: " );

hourInput = new TextField( 5 );

hourInput.addActionListener( this );

hourLabel = new Label( "Enter hours worked: " );

 

add( rateLabel );

add( rateInput );

add( hourLabel );

add( hourInput );

}

 

public void actionPerformed( ActionEvent e )

{

hours = Integer.parseInt( hourInput.getText() );

rate = Integer.parseInt( rateInput.getText() );

 

if ( hours > 40 )

pay = ( 40.0 * rate ) + ( hours - 40 ) * ( rate * 1.5 );

else

pay = hours * rate;

 

showStatus( "Pay is $" + pay );

}

}

 

// Exercise 2.15 Solution

// Largest.java

// Program determines and prints

// the largest of ten numbers

import java.applet.Applet;

import java.awt.*;

import java.io.*;

 

public class Largest {

 

public static void main( String args[] ) throws IOException

{

int max, number, count;

 

max = 0;

count = 2;

 

System.out.println( "Enter a single digit number: " );

number = System.in.read();

 

while ( count <= 10 ) {

if ( number > max )

max = number;

 

System.in.skip( 2 );

System.out.println( "Enter a single digit number: " );

number = System.in.read();

++count;

}

 

System.out.println( "Largest is " + ( char ) max );

}

}

 

// Exercise 2.16 Solution

// Table.java

// Program prints a table of values

 

public class Table {

 

public static void main( String args[] )

{

int n = 0;

 

System.out.println( "N\t10*N\t100*N\t1000*N" );

 

while ( ++n <= 5 )

System.out.println( n + "\t" + ( 10 * n ) +

"\t" + ( 100 * n ) + "\t"

+ ( 1000 * n ) );

}

}

 

// Exercise 2.17 Solution

// Largest.java

// Program determines and prints

// the largest of ten numbers

import java.applet.Applet;

import java.awt.*;

import java.io.*;

 

public class TwoLargest {

 

public static void main( String args[] ) throws IOException

{

int largest, nextLargest, number, count;

 

count = 3;

nextLargest = 0;

 

System.out.println( "Enter a single digit number: " );

largest = System.in.read();

 

System.in.skip( 2 );

System.out.println( "Enter a single digit number: " );

number = System.in.read();

 

while ( count <= 10 ) {

if ( number > largest ) {

nextLargest = largest;

largest = number;

}

else if ( number > nextLargest )

nextLargest = number;

 

System.in.skip( 2 );

System.out.println( "Enter a single digit number: " );

number = System.in.read();

++count;

}

 

System.out.println( "Largest is " + ( char ) largest );

System.out.println( "Second largest is " + ( char ) nextLargest );

}

}

 

// Exercise 2.18 Solution

// Analysis.java

// Analysis of examination results

import java.io.*;

 

public class Analysis {

 

public static void main( String args[] ) throws IOException

{

// initializing variables in declarations

int passes = 0, failures = 0, student = 1, result;

// process 10 students; counter-controlled loop

while ( student <= 10 ) {

System.out.print( "Enter result (1=pass,2=fail): " );

System.out.flush();

result = System.in.read();

if ( result == '1' ) { // if/else nested in while

passes = passes + 1;

student = student + 1;

}

else if ( result == '2' ) {

failures = failures + 1;

student = student + 1;

}

else

System.out.println( "Invalid input." );

 

System.in.skip( 2 );

}

System.out.println( "Passed " + passes );

System.out.println( "Failed " + failures );

if ( passes > 8 )

System.out.println( "Raise tuition " );

}

}