Programming Project Solutions
3.1 Average
//********************************************************************
// Average.java Author: Lewis/Loftus/Cocking
//
// Solution to Programming Project 3.1
//
// Demonstrates the use of a while loop, a sentinel value, and a
// running sum.
//********************************************************************
import java.text.DecimalFormat;
import java.util.Scanner;
public class Average
{
//------
// Computes the average of a set of values entered by the user.
// The running sum is printed as the numbers are entered.
//------
public static void main (String[] args)
{
int sum = 0, value, count = 0;
double average;
Scanner scan = new Scanner(System.in);
System.out.print ("Enter an integer (0 to quit): ");
value = scan.nextInt();
while (value != 0) // sentinel value of 0 to terminate loop
{
count++;
sum += value;
System.out.println ("The sum so far is " + sum);
System.out.print ("Enter an integer (0 to quit): ");
value = scan.nextInt();
}
System.out.println ();
System.out.println ("Number of values entered: " + count);
if (count < 1)
{
System.out.println("Cannot compute average, no values entered");
}
else
{
average = (double)sum / count;
DecimalFormatfmt = new DecimalFormat ("0.###");
System.out.println ("The average is " + fmt.format(average));
}
}
}
3.2 LeapYear
//********************************************************************
// LeapYear.java Author: Lewis/Loftus/Cocking
//
// Solution to Programming Project 3.2
//
// Reads an integer value representing a year. Determines if the year
// is a leap year
//********************************************************************
import java.util.Scanner;
public class LeapYear
{
//------
// Reads an integer value representing a year. Determines if the year
// is a leap year
//------
public static void main (String[] args)
{
int year;
boolean leap = false;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a year : ");
year = scan.nextInt();
if (year < 1582)
System.out.println("ERROR year not valid in the Gregorian calendar");
else
{
if (year % 4 == 0) // divisible by 4
{
leap = true;
if ((year % 100 == 0) & (year % 400 != 0))
{
leap = false;
}
}
if (leap)
System.out.println(year + " is a leap year");
else
System.out.println(year + " is not a leap year");
}
}
}
3.5 StringDown
//********************************************************************
// StringDown.java Author: Lewis/Loftus/Cocking
//
// Solution to Programming Project 3.5
//********************************************************************
import java.util.Scanner;
public class StringDown
{
//------
// Reads a string from the user and prints it one character
// per line.
//------
public static void main (String[] args)
{
String str;
Scanner scan = new Scanner(System.in);
System.out.println ("Enter a string of characters:");
str = scan.nextLine();
for (int index=0; index < str.length(); index++)
System.out.println (str.charAt(index));
}
}
3.6 CountDigits
//********************************************************************
// CountDigits.java Author: Lewis/Loftus/Cocking
//
// Solution to Programming Project 3.6
//********************************************************************
import java.util.Scanner;
public class CountDigits
{
//------
// Counts the number of odd, even, and zero digits in an
// integer input value.
//------
public static void main (String[] args)
{
intoddCount = 0, evenCount = 0, zeroCount = 0;
int value, digit;
Scanner scan = new Scanner(System.in);
System.out.print ("Enter an integer value: ");
value = scan.nextInt();
value = Math.abs (value);
if (value == 0)
zeroCount++;
while (value > 0)
{
digit = value % 10;
if (digit == 0)
zeroCount++;
else
if (digit%2 == 0)
evenCount++;
else
oddCount++;
value = value / 10;
}
System.out.println ("Zero digits: " + zeroCount);
System.out.println ("Even digits: " + evenCount);
System.out.println ("Odd digits: " + oddCount);
}
}
3.10 HiLo
//********************************************************************
// HiLo.java Author: Lewis/Loftus/Cocking
//
// Solution to Programming Project 3.10
//********************************************************************
import java.util.Scanner;
public class HiLo
{
//------
// Randomly selects a number in a particular range, which the
// user attempts to guess.
//------
public static void main (String[] args)
{
final int MAX = 100;
int target, count = 0, guess;
String again;
Scanner scan = new Scanner(System.in);
do
{
System.out.println();
System.out.println ("Guess a number between 1 and " + MAX);
target = (int) (Math.random() * MAX) + 1;
do
{
System.out.println();
System.out.print ("Enter your guess (0 to quit): ");
guess = scan.nextInt();
count = count + 1;
if (guess > 0)
if (guess == target)
System.out.println ("Right! Guesses: " + count);
else
if (guess < target)
System.out.println ("Your guess was too LOW.");
else
System.out.println ("Your guess was too HIGH.");
}
while (guess != target & guess > 0);
System.out.println();
System.out.print ("Play again (y/n)?: ");
again = scan.nextLine();
}
while (again.equalsIgnoreCase ("y"));
}
}
3.12a Stars2
//********************************************************************
// Stars2.java Author: Lewis/Loftus/Cocking
//
// Solution to Programming Project 3.12a
//********************************************************************
public class Stars2
{
//------
// Prints a triangle shape using asterisk (star) characters.
//------
public static void main (String[] args)
{
final int LIMIT = 10;
for (int row = 1; row <= LIMIT; row++)
{
for (int star = 1; star <= LIMIT-row+1; star++)
System.out.print ("*");
System.out.println();
}
}
}
3.12b Stars3
//********************************************************************
// Stars3.java Author: Lewis/Loftus/Cocking
//
// Solution to Programming Project 3.12b
//********************************************************************
public class Stars3
{
//------
// Prints a triangle shape using asterisk (star) characters.
//------
public static void main (String[] args)
{
final int LIMIT = 10;
for (int row = 1; row <= LIMIT; row++)
{
for (int space = 1; space <= LIMIT-row; space++)
System.out.print (" ");
for (int star = 1; star <= row; star++)
System.out.print ("*");
System.out.println();
}
}
}
3.12c Stars4
//********************************************************************
// Stars4.java Author: Lewis/Loftus/Cocking
//
// Solution to Programming Project 3.12c
//********************************************************************
public class Stars4
{
//------
// Prints a triangle shape using asterisk (star) characters.
//------
public static void main (String[] args)
{
final int LIMIT = 10;
for (int row = 1; row <= LIMIT; row++)
{
for (int space = 1; space <= row-1; space++)
System.out.print (" ");
for (int star = 1; star <= LIMIT-row+1; star++)
System.out.print ("*");
System.out.println();
}
}
}
3.12d Stars5
//********************************************************************
// Stars5.java Author: Lewis/Loftus/Cocking
//
// Solution to Programming Project 3.12d
//********************************************************************
public class Stars5
{
//------
// Prints a diamond shape using asterisk (star) characters.
//------
public static void main (String[] args)
{
final int LIMIT = 10;
// Print top half of diamond
for (int row = 1; row <= LIMIT/2; row++)
{
for (int space = 1; space <= (LIMIT/2)-row; space++)
System.out.print (" ");
for (int star = 1; star <= (row*2)-1; star++)
System.out.print ("*");
System.out.println();
}
// Print bottom half of diamond
for (int row = 1; row <= LIMIT/2; row++)
{
for (int space = 1; space <= row-1; space++)
System.out.print (" ");
for (int star = 1; star <= LIMIT-(row*2)+1; star++)
System.out.print ("*");
System.out.println();
}
}
}
3.13 Vowels
//********************************************************************
// Vowels.java Author: Lewis/Loftus/Cocking
//
// Solution to Programming Project 3.13
//********************************************************************
import java.util.Scanner;
public class Vowels
{
//------
// Counts the number of each vowel in a string.
//------
public static void main (String[] args)
{
intacount = 0, ecount = 0, icount = 0, ocount = 0, ucount = 0;
int other = 0;
Scanner scan = new Scanner(System.in);
System.out.println ("Enter a string of characters:");
String str = scan.nextLine();
str = str.toLowerCase(); // for consistent counting
for (int index = 0; index < str.length(); index++)
{
switch (str.charAt (index))
{
case 'a':
acount++;
break;
case 'e':
ecount++;
break;
case 'i':
icount++;
break;
case 'o':
ocount++;
break;
case 'u':
ucount++;
break;
default:
other++;
}
}
System.out.println ();
System.out.println ("Number of each vowel in the string:");
System.out.println ("a: " + acount);
System.out.println ("e: " + ecount);
System.out.println ("i: " + icount);
System.out.println ("o: " + ocount);
System.out.println ("u: " + ucount);
System.out.println ("other characters: " + other);
}
}
3.14 RockPaperScissors
//********************************************************************
// RockPaperScissors.java Author: Lewis/Loftus/Cocking
//
// Solution to Programming Project 3.14
//********************************************************************
import java.util.Scanner;
public class RockPaperScissors
{
//------
// Plays the Rock-Paper-Scissors game with the user.
//------
public static void main (String[] args)
{
final int OPTIONS = 3;
final int ROCK = 1, PAPER = 2, SCISSORS = 3;
final int COMPUTER = 1, PLAYER = 2, TIE = 3;
int computer, player, winner = 0;
int wins = 0, losses = 0, ties = 0;
String again;
Scanner scan = new Scanner(System.in);
do
{
computer = (int) (Math.random() * OPTIONS) + 1;
System.out.println();
System.out.print ("Enter your choice - 1 for Rock, 2 for " +
"Paper, and 3 for Scissors: ");
player = scan.nextInt();
System.out.print ("My choice was ");
// Determine the winner
switch (computer)
{
case ROCK:
System.out.println ("Rock.");
if (player == SCISSORS)
winner = COMPUTER;
else
if (player == PAPER)
winner = PLAYER;
else
winner = TIE;
break;
case PAPER:
System.out.println ("Paper.");
if (player == ROCK)
winner = COMPUTER;
else
if (player == SCISSORS)
winner = PLAYER;
else
winner = TIE;
break;
case SCISSORS:
System.out.println ("Scissors.");
if (player == PAPER)
winner = COMPUTER;
else
if (player == ROCK)
winner = PLAYER;
else
winner = TIE;
}
// Print results and increment appropriate counter
if (winner == COMPUTER)
{
System.out.println ("I win!");
losses++;
}
else
if (winner == PLAYER)
{
System.out.println ("You win!");
wins++;
}
else
{
System.out.println ("We tied!");
ties++;
}
System.out.println();
System.out.print ("Play again (y/n)?: ");
again = scan.nextLine();
}
while (again.equalsIgnoreCase ("y"));
// Print final results
System.out.println();
System.out.println ("You won " + wins + " times.");
System.out.println ("You lost " + losses + " times.");
System.out.println ("We tied " + ties + " times.");
}
}