Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Homework 3 Daniel Adams CSE292 1)SALES.JAVA package sales; import java.util.Scanner; public class sales { // calculates sales for 5 products public static void main( String args[] { double product1 = 0; // amount sold double product2 = 0; // amount sold double product3 = 0; // amount sold double product4 = 0; // amount sold double product5 = 0; // amount sold ) of of of of of first product second product third product fourth product fifth product /* Ask the user to enter product number */ System.out.println("Enter product number from 1 to 5"); Scanner input = new Scanner( System.in ); int number = 0; number = input.nextInt(); /* Create while statement that loops until sentinel is entered */ while(number != -99) { double quantity; if((number <= 5) && (number > 0)) { System.out.printf("Enter quantity for product "); System.out.println(number); quantity = input.nextInt(); switch (number) {//select case to multiply quantity and price for product case 1: product1 += quantity*2.98; break; case 2: product2 += quantity*4.50; break; case 3: product3 += quantity*9.98; break; case 4: product4 += quantity*4.49; break; case 5: product5 += quantity*6.87; break; }//end switch System.out.println("Enter another product number from 1 to 5"); number = input.nextInt(); // loop input through until sentinel value is entered }//end if else { //error message if number equals zero or is larger than 5 System.out.println("Please enter a number between 1 and 5"); number = input.nextInt(); // look for correct user input again } }//end while loop System.out.println(); //display totals of products System.out.printf( "Product 1: $%.2f\n", product1 ); System.out.printf( "Product 2: $%.2f\n", product2 ); System.out.printf( "Product 3: $%.2f\n", product3 ); System.out.printf( "Product 4: $%.2f\n", product4 ); System.out.printf( "Product 5: $%.2f\n", product5 ); }//end main } // end class Sales _________________________________________________ Sales.java running Enter product number from 1 to 5 1 Enter quantity for product 1 2 Enter another product number from 1 2 Enter quantity for product 2 3 Enter another product number from 1 3 Enter quantity for product 3 4 Enter another product number from 1 5 Enter quantity for product 5 5 Enter another product number from 1 33 Please enter a number between 1 and 0 Please enter a number to 5 to 5 to 5 to 5 5 2)TRIANGLE.JAVA package triangle_print; public class triangle { public static void main(String[] args) { //triangle (a) System.out.println("Triangle A\n"); for(int i = 1; i <= 10; i++) { for(int j = 1; j <=i; j++) { System.out.print('*'); } System.out.println(); } System.out.println("\n\n"); //triangle (b) System.out.println("Triangle B\n"); for(int i = 10; i >= 1; i--) { for(int j = 1; j <=i; j++) { System.out.print('*'); } System.out.println(); } System.out.println("\n\n"); //triangle (c) System.out.println("Triangle C\n"); for(int i = 10; i >= 1; i--) { for(int j = 10; j >= i ; j--) { System.out.print(' '); } for(int k = 1 ; k <= i; k++) { System.out.print('*'); } System.out.println(); } //triangle (d) System.out.println("Triangle D\n"); for(int i = 1; i <= 10; i++) { for(int j = 10; j >= i ; j--) { System.out.print(' '); } for(int k = 1 ; k <= i;k++) { System.out.print('*'); } System.out.println(); } } } ________________________________________________________________________ Triangle.Java Running Triangle A * ** *** **** ***** ****** ******* ******** ********* ********** Triangle B ********** ********* ******** ******* ****** ***** **** *** ** * Triangle C ********** ********* ******** ******* ****** ***** **** *** ** * Triangle D * ** *** **** ***** ****** ******* ******** ********* ********** 3)ROUNDING.JAVA package rounding_nums; public class rounding { public double roundToInteger(double number) { double y; y = Math.floor(number); return y; } public double roundToTenths(double number) { double y; y = Math.floor(number *10 + 0.5) / 10; return y; } public double roundToHundreths(double number) { double y; y = Math.floor(number *100 + 0.5) / 100; return y; } public double roundToThousandths(double number) { double y; y = Math.floor(number *1000 + 0.5) / 1000; return y; } public static void main(String[] args) { rounding round = new rounding(); double rounded = 9.5578; double final1 = round.roundToInteger(rounded); double final2 = round.roundToTenths(rounded); double final3 = round.roundToHundreths(rounded); double final4 = round.roundToThousandths(rounded); System.out.print("Round Integer:"); System.out.println(final1); System.out.print("Round Integer:"); System.out.println(final2); System.out.print("Round Integer:"); System.out.println(final3); System.out.print("Round Integer:"); System.out.println(final4); } } ______________________________________________________________ Rounding.Java Running Round Round Round Round Integer:9.0 Integer:9.6 Integer:9.56 Integer:9.558 4)REVERSETEST.JAVA package reverse_digits; import java.util.Scanner; public class reverseTest { public void reverse(int x) { int last; int reversed = 0; while(x > 0) { last = x % 10; //find last digit reversed = (reversed * 10) + last; //hold value of reverse by adding on the last value x = x / 10; // remove last digit from the users input until it == 0 } System.out.println ("The reverse of your number is " + reversed); } public static void main(String[] args) { System.out.print("Input integers to reverse: "); Scanner input = new Scanner( System.in ); int number; number = input.nextInt(); reverseTest reverse_int = new reverseTest(); // create new object reverse_int.reverse(number);//call reverse method } } ___________________________________________________________ ReverseTest.Java Running Input integers to reverse: 123456789 The reverse of your number is 987654321 5)DICEROLL_TEST.JAVA package dice_roll; import java.util.Random; public class dice_rollTest { public static void main(String[] args) { Random randomNumbers = new Random(); //Random randomNumbers2 = new Random(); int[] frequency = new int [13]; //array of frequency counters //roll die 6,000,000 times; use die value as frequency index for(int roll = 1;roll <= 36000000;roll++)//roll 36,000,000 times in order to get an accurate result { ++frequency[ (1 + randomNumbers.nextInt(6)) + (1 + randomNumbers.nextInt(6))];//add 1 to every each number that comes up } System.out.printf("%s%10s\n", "Face","Frequency");//print labels for array table //output each array element's value for(int face = 2; face < frequency.length;face++)//start from 2 because one will never come up { System.out.printf("%4d%10d\n", face, frequency[face]);// display frequency to user } } } ___________________________________________________________________ DICEROLL_TEST.Java Running Face Frequency 2 1000101 3 2002372 4 2999720 5 4000146 6 4997478 7 5998995 8 4999324 9 3998535 10 3000181 11 2001014 12 1002134 6)RANDOM_TEST.JAVA package random_shapes; import javax.swing.JFrame; public class random_test { public static void main( String[] args ) { // create a panel that contains our drawing random_draw panel = new random_draw(); // create a new frame to hold the panel JFrame application = new JFrame(); // set the frame to exit when it is closed application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); application.add( panel ); // add the panel to the frame application.setSize( 450, 450 ); // set the size of the frame application.setVisible( true ); // make the frame visible } // end main } // end class DrawPanelTest ======================================================================== RANDOM_DRAW.JAVA package random_shapes import java.awt.Color; import java.util.Random; import java.awt.Graphics; import javax.swing.JPanel; public class random_draw extends JPanel { public void paintComponent( Graphics g )// circle { // call paintComponent to ensure the panel displays correctly super.paintComponent( g ); Random randomNumbers = new Random(); Random rcolor = new Random(); for(int i = 1; i <= 10; i++) { //generate random numbers to choose the shape int rand = 1 + randomNumbers.nextInt( 2 ); //random color generator int r = rcolor.nextInt(255); int g1 = rcolor.nextInt(255); int b = rcolor.nextInt(255); //set random color value Color randomColor = new Color(r, g1, b); //random dimensions int x = randomNumbers.nextInt(getWidth()-200); int y = randomNumbers.nextInt(getHeight()-200); int width = randomNumbers.nextInt(getWidth()/2); int height = randomNumbers.nextInt(getHeight()/2); switch( rand )//choose oval or rectange from random number { case 1://print oval g.setColor(randomColor); g.fillOval(x, y, width, height); break; case 2://print rectangle g.setColor(randomColor); g.fillRect(x, y, width, height); break; } } } } _____________________________________________________________ Random_TEST.Java Running =========================== I had no problem finishing each part of the assignment successfully while spending a good amount of time on each program. Score: 80/80