Unit 2 sample programs
Illustrating Output: Ass2s1p2.java 
|
Write a program that uses stars (*) to print your initials; a diamond, a triangle or any other interesting shape
|
// The "Ass2s1p2" class.
import java.awt.*;
import hsa.Console;
public class Ass2s1p2
{
static Console cs; // The output console
public static void main (String[] args)
{
cs = new Console ();
cs.println(" ******** ********* ******");
cs.println(" **** ***** ***");
cs.println(" **** ***** ***");
cs.println(" **** ***** ***");
cs.println(" **** ***** ***");
cs.println(" **** ********");
cs.println(" **** ********");
cs.println(" **** ***** ***");
cs.println("******** **** ***** ***");
cs.println(" **** **** ***** ***");
cs.println(" ******** ***** ***");
cs.println(" **** ********* ******");
} // main method
} // Ass2s1p2 class
|
Illustrating Order of Operations: Ass2s1p5.java 
|
(Careful how you mix integers with reals: 5/9 = 0)
Write a program that lets you enter a degree measure in Fahrenheit, and that
prints the result in celsius degree measure: c = 5/9( f - 32)
|
// The "Ass2s1p5" class.
import java.awt.*;
import hsa.Console;
public class Ass2s1p5
{
static Console cs;
public static void main (String[] args)
{
cs = new Console ();
double fahr, cels;
cs.print("Enter a Fahrenheit degree measure: ");
fahr=cs.readDouble();
cels = (fahr-32)/1.8;
cs.print(fahr,5,2);
cs.print(" degrees Fahrenheit = ");
cs.print(cels,5,2);
cs.println(" degrees Celsius");
} // main method
} // Ass2s1p5 class
|
Illustrating MOD: Ass2s1p7.java 
|
Write a program that lets you enter a number of hours, and that converts it to days
and hours. For example, 111 hours = 4 days and 15 hours.
|
// The "Ass2s1p7" class.
import java.awt.*;
import hsa.Console;
public class Ass2s1p7
{
static Console cs;
public static void main (String[] args)
{
cs = new Console();
int hrs,days,leftOverHrs;
cs.print("enter a number of hours: ");
hrs = cs.readInt();
days=hrs/24;
leftOverHrs=hrs%24;
cs.println (hrs+" hours = "+days+" days and "+leftOverHrs+" hours");
} // main method
} // Ass2s1p7 class
|
Illustrating ACCUMULATION: Ass2s2p6.java 
|
Write a program that prints the sum of the 5 numbers that you entered:
|
// The "Ass2s2p6" class.
// find the sum of 5 numbers
import java.awt.*;
import hsa.Console;
public class Ass2s2p6
{
static Console cs;
public static void main (String [] args)
{
cs = new Console ();
int sum = 0;
for (int i = 1 ; i <= 5 ; i++)
{
cs.print ("enter number " + i + " of 5: ");
int x = cs.readInt ();
sum += x;
}
cs.println ("\n\nthe sum is " + sum);
} // main method
} // Ass2s2p6 class
|
Illustrating LARGEST: Ass2s2p8.java 
|
Write a program that prints the largest of the 5 numbers that you entered
|
// The "Ass2s2p8" (combined with "Ass2s2p8" class).
// find the sum of 5 numbers
import java.awt.*;
import hsa.Console;
public class Ass2s2p8
{
static Console cs;
public static void main (String [] args)
{
cs = new Console ();
int sum = 0;
int m = -999999999;
for (int i = 1 ; i <= 5 ; i++)
{
cs.print ("enter number " + i + " of 5: ");
int x = cs.readInt ();
sum += x;
m = Math.max (m, x);
}
cs.println ("\nand the largest is " + m);
} // main method
} // Ass2s2p8 class
|
Illustrating MANIPULATING STRINGS: Ass2s2p13.java 
|
Write a program that lets you enter a word, such as: Canada
and that prints out the word in various ways:
|
// The "Ass2s2p13" class.
import java.awt.*;
import hsa.Console;
public class Ass2s2p13
{
static Console cs;
public static void main (String [] args)
{
cs = new Console ();
cs.print ("enter a NAME: ");
String name = cs.readString ();
int n = name.length ();
for (int i = n ; i > 0 ; i--)
cs.println (name.substring (0, i));
} // main method
} // Ass2s2p13 class
|
Illustrating SELECTION: Ass2s3p3.java 
|
Write a program that will let you enter 3 sides of a triangle. if each of the three
sides is less than half the perimeter, then a triangle can be formed (2,3, 1000 can
not form a triangle) If a triangle can be formed, find the area of the triangle. If no
triangle can be formed, say so.
|
// The "Ass2s3p3" class.
import java.awt.*;
import hsa.Console;
public class Ass2s3p3
{
static Console cs;
public static void main (String [] args)
{
cs = new Console ();
double a, b, c, s, area;
cs.println ("Enter the three sides of a triangle");
cs.print ("Enter the first side: ");
a = cs.readDouble ();
cs.print ("Enter the second side: ");
b = cs.readDouble ();
cs.print ("Enter the third side: ");
c = cs.readDouble ();
s = (a + b + c) / 2.0;
if ((s > a) && (s > b) && (s > c))
{
area = Math.sqrt (s * (s - a) * (s - b) * (s - c));
cs.print ("\nThe area of your triangle is ");
cs.println (area, 2, 2);
}
else
cs.println ("those numbers cannot form a triangle");
} // main method
} // Ass2s3p3 class
|
Illustrating DO-WHILE LOOP: Ass2s4p4.java 
|
Write a program that lets the computer generate a secret number (an integer
between 1 and 100, for your friend to guess. If they guess too high, print: "too high,
guess again." If they guess too low, print: "too low, guess again" If they guess the
number, let the program stop and print "congratulations"
|
// The "Ass2s4p4" class.
import java.awt.*;
import hsa.Console;
public class Ass2s4p4
{
static Console cs;
public static void main (String [] args)
{
cs = new Console ();
int rand = (int) (Math.random () * 100 + 1);
int guess;
do
{
cs.print ("enter your guess: ");
guess = cs.readInt ();
if (guess < rand)
cs.println (guess + " is too low");
if (guess > rand)
cs.println (guess + " is too high");
}
while (guess != rand);
cs.println ("You guessed it! " + guess + " is correct");
} // main method
} // Ass2s4p4 class
|
Illustrating WHILE: Ass2s4p9.java 
|
Write a program that lets you enter two integers and that prints the greatest
common divisor of these integers. Note that the greatest common divisor of two
numbers is the second last number in the sequence of numbers defined as
follows: the first number is the largest of the two, the next number is the smallest
of the two, each succeeding number is the remainder when the second last
number is divided by the last number. The sequence ends with 0.
It is called the Euclidian algorithm.
Example: The GCD of 45 and 55 is 5, from the sequence: 55, 45, 10, 5, 0
The GCD of 222 and 90 is 6 from the sequence: 222, 90, 42, 6, 0
|
// The "Ass2s4p9" class.
import java.awt.*;
import hsa.Console;
public class Ass2s4p9
{
static Console cs;
public static void main (String [] args)
{
cs = new Console ();
cs.println ("Find the greatest common divisor of two numbers");
cs.print ("Enter the first positive integer: ");
int a = cs.readInt ();
cs.print ("Enter the second positive integer: ");
int b = cs.readInt ();
cs.print ("The GCD of " + a + " and " + b + " is: ");
int c;
while (b != 0)
{
c = a % b;
a = b;
b = c;
}
cs.print (a);
} // main method
} // Ass2s4p9 class
|
Illustrating STRING METHODS: Ass2s4p14.java 
|
Write a program that lets you enter a word and that prints out the number of vowels
and the number of consonants (vowels are: a,e,i,o,u. all others are consonants)
The program should repeat asking for more words, until you enter "stop"
|
// The "Ass2s4p14" class.
import java.awt.*;
import hsa.Console;
public class Ass2s4p14
{
static Console cs;
public static void main (String [] args)
{
cs = new Console ();
cs.println ("Find the number of consonants and vowels");
cs.print ("Enter a word or \"stop\" ");
String word = cs.readString ();
String vowels = "AEIOU";
String upword = word.toUpperCase ();
String consonants = "BCDFGHJKLMNPQRSTVWXYZ";
while (!upword.equals ("STOP"))
{
int vsum = 0, csum = 0;
for (int i = 0 ; i < upword.length () ; i++)
{
// check each character in upword to see
// what position it has in vowels: position 0..4 (no position=-1)
if (vowels.indexOf (upword.charAt (i)) >= 0)
vsum++;
// check each character in upword to see
// what position it has in consonants: position 0..20
if (consonants.indexOf (upword.charAt (i)) >= 0)
csum++;
}
cs.println (word + " has " + vsum + " vowels and " + csum +
" consonants");
cs.print ("\nEnter a word or \"stop\" ");
word = cs.readString ();
upword = word.toUpperCase ();
}
} // main method
} // Ass2s4p14 class
|
Illustrating ARRAYS: Ass2s4p15.java 
|
Write a program that lets you enter an integer and that will translate it to its spoken
form from right to left. Example:
Enter a number: 32457
32457 = seven five four two three
|
// The "Ass2s4p15" class.
import java.awt.*;
import hsa.Console;
public class Ass2s4p15
{
static Console cs;
public static void main (String [] args)
{
cs = new Console ();
String digit [] = {"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};
int num, remainder;
cs.print ("enter a positive integer (0 to stop)");
num = cs.readInt ();
while (num > 0)
{
cs.print (num + " = ");
while (num != 0)
{
remainder = num % 10;
num /= 10;
cs.print (digit [remainder] + " ");
} //end while - process individual digits
cs.print ("\nenter a positive integer (0 to stop)");
num = cs.readInt ();
} //end while - process the entire number
} // main method
} // Ass2s4p15 class
|
|
Sponsored by ECOO and SIG-Computer Science
.
|
|
|