|
CSC 148 Java Programming // Exercise 3.6 Solution // Small.java // Program finds the smallest of several letters import java.io.*; public class Small { public static void main( String args[] ) throws IOException { int smallest = 'Z', temp, number; System.out.println( "Enter a digit followed by characters" ); // subtract unicode value of 48 (the value of // character '0') to get the actual numeric value number = System.in.read() - 48; smallest = System.in.read();
for ( int x = 2; x <= number; x++ ) { temp = System.in.read();
if ( temp < smallest ) smallest = temp; }
char smallest2 = ( char ) smallest; System.out.println( "Smallest letter is " + smallest2 ); } }
// Exercise 3.8 Solution // Factorial.java // Program calculates factorials import java.applet.Applet; import java.awt.*; public class Factorial extends Applet { public void paint( Graphics g ) { int fact, y = 20; g.drawString( "X", 5, 10 ); g.drawString( "X!", 70, 10 ); for ( int z = 1; z <= 5; z++ ) { fact = 1; for ( int w = 1; w <= z; w++ ) fact *= w; g.drawString( String.valueOf( z ), 5, y ); g.drawString( String.valueOf( fact ), 70, y ); y += 10; } } }
// Exercise 3.9 Solution // Interest.java // Calculating compound interest // NOTE: the Math.floor method is used to round // a number to a specific decimal place. It is // used here to round the number to the hundredths. import java.applet.Applet; import java.awt.*; public class Interest extends Applet { public void paint( Graphics g ) { double amount, principal = 1000.00, rate; int yPos = 40, xPos = 60; g.drawString( "Year", 25, 25 ); for ( rate = 0.05; rate <= 0.10; rate += .01 ) { rate = Math.floor( rate * 100 + 0.5 ) / 100; g.drawString( String.valueOf( rate ), xPos, 25 );
for ( int year = 1; year <= 10; year++ ) { amount = principal * Math.pow( 1.0 + rate, year ); amount = Math.floor( amount * 100 + 0.5 ) / 100; g.drawString( Integer.toString( year ), 25, yPos ); g.drawString( Double.toString( amount ), xPos, yPos ); yPos += 15; } yPos = 40; xPos += 60; } } }
// Exercise 3.10 Solution // Triangles.java // Program prints triangles public class Triangles { public static void main( String args[] ) { int row, column, space; // part a for ( row = 1; row <= 10; row++ ) { for ( column = 1; column <= row; column++ ) System.out.print( '*' ); System.out.println(); } System.out.println(); // part b for ( row = 10; row >= 1; row-- ) { for ( column = 1; column <= row; column++ ) System.out.print( '*' ); System.out.println(); } System.out.println(); // part c for ( row = 10; row >= 1; row-- ) { for ( space = 10; space > row; space-- ) System.out.print( ' ' ); for ( column = 1; column <= row; column++ ) System.out.print( '*' ); System.out.println(); } System.out.println(); // part d for ( row = 10; row >= 1; row-- ) { for ( space = 1; space < row; space++ ) System.out.print( ' ' ); for ( column = 10; column >= row; column-- ) System.out.print( '*' ); System.out.println(); } } }
// Exercise 3.11 Solution // Graphs.java // Program prints histograms import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Graphs extends Applet implements ActionListener { TextField input; Label inputLabel; boolean enablePaint; int count, num1, num2, num3; int num4, num5, temp; public void init() { input = new TextField( 4 ); input.addActionListener( this ); inputLabel = new Label( "Enter number:" ); add( inputLabel ); add( input ); } public void actionPerformed( ActionEvent e ) { enablePaint = false; temp = Integer.parseInt( input.getText() ); if ( temp >= 1 && temp <= 30 ) { ++count; showStatus( "Entry number " + count ); switch ( count ) { case 1: num1 = temp; break; case 2: num2 = temp; break; case 3: num3 = temp; break; case 4: num4 = temp; break; case 5: num5 = temp; break; } if ( count == 5 ) enablePaint = true; } else showStatus( "Number must be between 1 and 30!" ); repaint(); } public void paint( Graphics g ) { if ( enablePaint == true ) { int z, x, y = 0, value = 0; for ( ; count > 0; count-- ) { x = 5; switch ( count ) { case 1: y = 50; value = num1; break; case 2: y = 60; value = num2; break; case 3: y = 70; value = num3; break; case 4: y = 80; value = num4; break; case 5: y = 90; value = num5; break; } for ( z = 1; z <= value; z++ ) g.drawString( "*", x += 5, y ); } } } }
// Exercise 3.12 Solution // Sales.java // Program calculates sales import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Sales extends Applet implements ActionListener { TextField input, input2; Label inputLabel, inputLabel2; float product1, product2, product3; float product4, product5; public void init() { input = new TextField( 4 ); input2 = new TextField( 4 ); input2.addActionListener( this ); inputLabel = new Label( "Enter product number (1-5):" ); inputLabel2 = new Label( "Enter quantity sold: " ); add( inputLabel ); add( input ); add( inputLabel2 ); add( input2 ); } public void actionPerformed( ActionEvent e ) { showStatus( "" ); if ( e.getSource() == input2 ) { int productId = Integer.parseInt( input.getText() ); int temp = Integer.parseInt( input2.getText() ); if ( productId >= 1 && productId <= 5 ) {
switch ( productId ) { case 1: product1 += temp * 2.98; break; case 2: product2 += temp * 4.50; break; case 3: product3 += temp * 9.98; break; case 4: product4 += temp * 4.49; break; case 5: product5 += temp * 6.87; break; } } else showStatus( "Product number must be between 1 and 5!" ); } repaint(); } public void paint( Graphics g ) { g.drawString( "Totals", 5, 80 ); g.drawString( "Product 1: ", 5, 90 ); g.drawString( "$ " + product1, 100, 90 ); g.drawString( "Product 2: ", 5, 100 ); g.drawString( "$ " + product2, 100, 100 ); g.drawString( "Product 3: ", 5, 110 ); g.drawString( "$ " + product3, 100, 110 ); g.drawString( "Product 4: ", 5, 120 ); g.drawString( "$ " + product4, 100, 120 ); g.drawString( "Product 5: ", 5, 130 ); g.drawString( "$ " + product5, 100, 130 ); } }
// Exercise 3.19 Solution // Triangles.java // Program prints triangles public class Triangles { public static void main( String args[] ) { int row, column, space; for ( row = 1; row <= 10; row++ ) { // part a for ( column = 1; column <= row; column++ ) System.out.print( '*' ); for ( space = 1; space <= 10 - row; space++ ) System.out.print( ' ' ); System.out.print( "\t" ); // part b for ( column = 10; column >= row; column-- ) System.out.print( '*' ); for ( space = 1; space < row; space++ ) System.out.print( ' ' ); System.out.print( "\t" ); // part c for ( space = 10; space > row; space-- ) System.out.print( ' ' ); for ( column = 1; column <= row; column++ ) System.out.print( '*' ); System.out.print( "\t" ); // part d for ( space = 1; space < row; space++ ) System.out.print( ' ' ); for ( column = 10; column >= row; column-- ) System.out.print( '*' ); System.out.println(); } } }
|