Download CSE 292 1) Sales.java • I have finished the task successfully. 10/10

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Kimberly Juan – HW3 – CSE 292
1) Sales.java
 I have finished the task successfully. 10/10
package Sales;
// Sales.java
// Program calculates sales, based on an input of product
// number and quantity sold
import java.util.Scanner;
public class Sales
{
// calculates sales for 5 products
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );
double
double
double
double
double
product1
product2
product3
product4
product5
=
=
=
=
=
0;
0;
0;
0;
0;
//
//
//
//
//
amount
amount
amount
amount
amount
sold
sold
sold
sold
sold
of
of
of
of
of
first product
second product
third product
fourth product
fifth product
int number = 0;
/* Ask the user to enter product number */
System.out.println("Please enter a product number 1 - 5 (-1 to quit):
");
number = input.nextInt();
/* Create while statement that loops until sentinel is entered */
while(number != -1)
{
double quantity;
/* Determine whether user's product number is 1-5 */
if ((number <= 5) && (number > 0))
{
/* If so ask user to input the quantity sold */
System.out.printf("How many sold for product", number,
"?");
quantity = input.nextInt();
/* Write a switch statement here that will compute the
total for that product */
switch (number)
{
case 1:
product1 += quantity*2.99;
break;
case 2:
product2 += quantity*3.99;
break;
case 3:
product3 += quantity*8.49;
break;
case 4:
product4 += quantity*4.99;
break;
case 5:
product5 += quantity*6.99;
break;
} // end switch
}
/* If product number is not in 1-5, test if product number
is not 0 */
else
{
/* Display error message for invalid product number */
System.out.println("Invalid product number.");
}
/* Ask the user to enter another product number */
System.out.println("Please enter another product number 1 - 5 ( 1 to quit):");
number = input.nextInt();
} /* end while loop */
// print summary
System.out.println();
System.out.printf("Product
System.out.printf("Product
System.out.printf("Product
System.out.printf("Product
System.out.printf("Product
1:
2:
3:
4:
5:
$%.2f\n",
$%.2f\n",
$%.2f\n",
$%.2f\n",
$%.2f\n",
product1);
product2);
product3);
product4);
product5);
/* write code here for the rest of the summary message it should contain
the totals for the rest of the products, each on its own line*/
} //end main
}
// end class sales
2) Triples.java
 I have finished the task successfully. 10/10
package Triples;
//Lab 3: Triples.java
//Program calculates Pythagorean triples
public class Triples
{
public static void main( String args[] )
{
// declare the three sides of a triangle
int side1;
int side2;
int hypotenuse;
/* Write loop for side1 to try the values 1-500. */
for (side1 = 1; side1 <= 500; side1++)
{
/* Write loop for side2 to try the values 1-500. */
for (side2 = 1; side2 <= 500; side2++)
{
/* Write loop for hypotenuse to try the values 1-500 */
for (hypotenuse = 1; hypotenuse <= 500; hypotenuse++)
{
/* Write an if statement that determines whether the sum of the
two sides squared equals the hypotenuse squared. If this
condition is true display side1, side2 and hypotenuse. */
if ( ((side1*side1) + (side2*side2)) ==
(hypotenuse*hypotenuse))
{
System.out.printf("Side 1: %s\n", side1);
System.out.printf("Side 2: %s\n", side2);
System.out.printf("Hypotenuse: %s\n", hypotenuse);
}
}
}
}
} // end main
} // end class Triples
3) Multiply.java
 I have finished the task successfully. 10/10
// Lab 3: Multiply.java
// Program generates single digit multiplication problems
import java.util.*;
public class Multiply
{
Random randomNumbers = new Random();
int answer; // the correct answer
// ask the user to answer multiplication problems
public void quiz()
{
Scanner input = new Scanner( System.in );
int guess; // the user's guess
/* write code to call method checkResponse to display the question */
createQuestion();
System.out.println( "Enter your answer (-1 to exit):" );
guess = input.nextInt();
while ( guess != -1 )
{
/* write code to call the method to check the user's answer */
checkResponse(guess);
System.out.println( "Enter your answer (-1 to exit):" );
guess = input.nextInt();
} // end while
} // end method
// prints a new question and stores the corresponding answer
/* write method header for the createQuestion method */
public void createQuestion ()
{
// get two random numbers between 0 and 9
/* Write code to get two random numbers and store them in variables
digit1 and digit2. */
int digit1 = randomNumbers.nextInt(10);
int digit2 = randomNumbers.nextInt(10);
/* Write code to multiply the two variables and store the result
in variable answer */
answer = digit1*digit2;
System.out.printf( "How much is %d times %d?\n", digit1, digit2 );
} // end method createQuestion
// checks if the user answered correctly
/* Write method header for checkResponse */
public void checkResponse(int x)
{
/* Write code to test whether the answer is incorrect */
if(x != answer)
{
/* Write code to tell the user to try again, if the answer is
incorrect */
System.out.println("Incorrect answer. Please try again (-1 to
exit)");
}//end if
else
{
System.out.println( "Very Good!" );
/* Write code to call method createQuestion to display another question */
createQuestion();
}
} // end method checkResponse
} // end class Multiply
4) Triangle.java
 I have finished the task successfully. 10/10
//Triangle.java
//October 31, 2012
public class Triangle
{
public static void main (String[] args)
{
System.out.println("Triangle A\n");
int x, y;
/* nested loop to print triangle*/
for(x = 0; x <= 10; x++)
{
for(y = 0; y <= x; y++){
System.out.print("*");
}
System.out.println();
}
System.out.println("\nTraingle B\n");
/* nested loop to print triangle*/
for(x = 10; x >= 0; x--)
{
for (y=0; y <= x; y++)
{
System.out.print("*");
}
System.out.println();
}
System.out.println("\nTriangle C\n");
/* nested loop to print triangle*/
for(x = 10; x >= 0; x--)
{
for(y = 10; y >= x; y--){
System.out.print(" ");
}
for(int k = 1; k <= x; k++)
{
System.out.print("*");
}
System.out.println();
}
System.out.println("\nTraingle D\n");
/* nested loop to print triangle*/
for(x = 1; x <= 10; x++)
{
for (y=10; y >= x; y--)
{
System.out.print(" ");
}
for (int k = 1; k <=x; k++)
{
System.out.print("*");
}
System.out.println();
}
}//end main
} // end class
5) RoundingNumbers.java
 I have finished the task successfully. 10/10
6) // RoudndingNumbers.java
7) // 10/31/12
8)
9) public class RoundingNumbers
10) {
11)
public double roundToInteger(double number)
12)
{
13)
double x;
14)
x = Math.floor(number);
integer
15)
return x;
16)
}
17)
18)
public double roundToTenth(double number)
19)
{
20)
double x;
21)
x = Math.floor( number * 10 + 0.5 ) / 10;
tenths
22)
return x;
23)
}
24)
25)
public double roundToHundredths(double number)
26)
{
27)
double x;
// rounds to an
//rounds to
28)
29)
30)
31)
32)
33)
34)
35)
36)
37)
38)
39)
40)
41)
42)
43)
44)
45)
46)
47)
48)
49)
50)
51)
52)
53)
54)
55)
x = Math.floor( number * 100 + 0.5 ) / 100;
//rounds to hundredths
return x;
}
public double roundToThousandths(double number)
{
double x;
x = Math.floor( number * 1000 + 0.5 ) / 1000;
//rounds to thousandths
return x;
}
public static void main(String[] args)
{
RoundingNumbers round = new RoundingNumbers();
double number = 10.8746;
System.out.printf("Original number: %s\n\n", number);
System.out.println("Rounded to\n");
System.out.printf("Integer: %s\n", round.roundToInteger(number));
System.out.printf("Tenths: %s\n", round.roundToTenth(number));
System.out.printf("Hundredths: %s\n",
round.roundToHundredths(number));
System.out.printf("Thousandths: %s\n",
round.roundToThousandths(number));
56)
57)
} // end main
58) } // end class
6) ReversingDigits.java
 I have finished the task successfully. 10/10
//ReversingDigits.java
//10/21/12
import java.util.Scanner;
public class ReversingDigits
{
public int reverseDigits ( int n )
{
// reverses a number
int r = 0;
while ( n != 0 )
{
r = r * 10 + n % 10;
n = n / 10;
//makes r the new reverse digit
// find n minus the last number
}
return r;
}
public static void main(String [] args)
{
Scanner input = new Scanner( System.in );
System.out.println("Please enter a value:"); //prompts user for a number
int number;
number = input.nextInt();
//user input
ReversingDigits reverse = new ReversingDigits(); // creates new object
System.out.printf("Reversed Digits are: %s\n",
reverse.reverseDigits(number) ); //prints the new reversed digit
} //end main
}// end class