COMP 274 Week 1 Lab
$9.00
Description
Do the following two programs:
Write a Java application that inputs three integers from the user and displays the sum, average, product, smallest and largest of the numbers. Use a Scanner object to get the input from the user. Use System.out to output the results. The average should be a floating point result, and should be displayed with 2 digits after the decimal point.
Write a Java application which tests the random number generation abilities of Java. Random number generation is explained below.Your application should simulate rolling a pair of dice some number of times. Use a JOptionPane to ask the user how many times the dice should be rolled. Each dice roll can be a value between 1 and 6, so rolling 2 dice will result in values between 2 and 12. You need to generate a random number between 1 and 6 to simulate rolling each dice. Keep track of the result of each roll using an array that is indexed by the sum of the roll of the two dice. Output your result in a table which shows each value (ie. 2 – 12) and the number of times that value was rolled.
The program must have the following structure:
The main function declares the counting array and gathers input from the user.
A separate function is called which takes an integer array parameter as well as the number of dice rolls to execute. This function rolls the dice the required number of times and uses the array provided to count the number of times each value is rolled.
The main program takes the count data from the array, formats it and displays it as described above.
For output, you should build a formatted string which includes tabs and new lines to represent your table. JOptionPanes by themselves are not capable of properly formatting tabs. However, there is another Java class which can be used with JOptionPanes to correctly display a formatted string. The JTextArea class understands all string formatting instructions. The following illustrates how to use the JTextArea class with a JOptionPane to display a formatted string.
#import javax.swing.JOptionPane; #import javax.swing.JTextArea; String output = “This t is t a t formatted t string.
”; // Create a new JTextArea object JTextArea area = new JTextArea( ); // Place a formatted string into the JTextArea area.setText( output ); // Place the text area into the JOptionPane JOptionPane.showMessageDialog( null, area, “Title for dialog box”, JOptionPane.INFORMATION_MESSAGE );
For each of the two programming problems, create an Eclipse project and develop a Java program to solve the problem. Make sure to capture a sample of your program’s output. The best way to do this is to click on the console window you want to capture and then press the Alt and PrintScreen keys at the same time. Then paste your captured screen image into a Word document. For each of the two programs, put the screen capture followed by a copy of your source code into your Word document.
Random Numbers in Java
There are two different classes available to generate random numbers in Java. The static method random from the Math class can be used to generate a random floating point number from 0 up to but not including 1. This number can be scaled to whatever range of random numbers is desired.
(int)(Math.random( ) * 10); //results in an integer between 0 and 9
The Random class from the java.util package can also be used to get random numbers. The nextInt method returns an integer from 0 up to but not including the argument value provided to the nextInt method.
Random rand = new Random( ); int number = rand.nextInt(10); //results in an integer between 0 and 9 minimumValue + differenceBetweenValues * rand.nextInt( scalingFactor )
The formula shown above is used to generate a range of random numbers starting at some minimum value and generating some specified number of random values. The scalingFactor determines how many different random values will be generated by the call to nextInt, starting with the value 0. The difference between values is usually 1.
Your final programming document should contain in the following order:
Screen capture of the first program followed by source code.
Screen capture of the second program followed by source code.
Submit this as the Week 1 Programming Solution.
SCREENSHOTS
SOLUTION
PAYMENT
ENTIRE COURSE
The solution includes a zip file.
Attachments [Move over files to preview content of those files]
COMP274_Lab_1.zip (271.54 KB)
COMP274Lab1_Average_Screenshot.png
COMP274Lab1_Document.docx
COMP274Lab1_RollingDice_Screenshot.png
java source code
Average.java
SimulateRollingDice.java
Netbean project
COMP274Lab1
build
classes
.netbeans_automatic_build
.netbeans_update_resources
Average.class
SimulateRollingDice.class
build.xml
manifest.mf
nbproject
build-impl.xml
genfiles.properties
private
private.properties
project.properties
project.xml
src
Average.java
SimulateRollingDice.java
Preview Average.java
xxxxxx xxxx.xxxx.xxxxxxx;
xxxxxx xxxxx xxxxxxx {
xxxxxx xxxxxx xxxx xxxx(xxxxxx xxxx[]) {
xxxxxxx xxxxx = xxx xxxxxxx(xxxxxx.xx);
int[] number = new int[3]; int sum = 0; int product = 1; int smallest = Integer.MAX_VALUE; int largest = Integer.MIN_VALUE; System.out.print(“Please enter three integers: “); for (int i = 0; i < 3; i++) {
xxxxxx[x] = xxxxx.xxxxxxx();
xxx += xxxxxx[x];
xxxxxxx *= xxxxxx[x];
xxxxxxxx = xxxxxxxx < xxxxxx[x] ? xxxxxxxx : xxxxxx[x];
xxxxxxx = xxxxxxx > xxxxxx[x] ? xxxxxxx : xxxxxx[x];
}
Preview SimulateRollingDice.java
xxxxxx xxxx.xxxx.xxxxxx;
xxxxxx xxxxx.xxxxx.xxxxxxxxxxx;
xxxxxx xxxxx.xxxxx.xxxxxxxxx;
public class SimulateRollingDice {
public static void main(String args[]) { Random rand = new Random(System.currentTimeMillis()); int counter[] = new int[13];
String input = JOptionPane.showInputDialog(null,
“xxx xxxx xxxxx xxx xxxx xxxxxx xx xxxxxx?”, “xxxxx”,
xxxxxxxxxxx.xxxxxxxxxxx_xxxxxxx);
xxx x = xxxxxxx.xxxxxxxx(xxxxx);
xxx (xxx x = 0; x < x; x++) {
Preview Average.java
xxxxxx xxxx.xxxx.xxxxxxx;
xxxxxx xxxxx xxxxxxx {
xxxxxx xxxxxx xxxx xxxx(xxxxxx xxxx[]) {
xxxxxxx xxxxx = xxx xxxxxxx(xxxxxx.xx);
int[] number = new int[3]; int sum = 0; int product = 1; int smallest = Integer.MAX_VALUE; int largest = Integer.MIN_VALUE; System.out.print("Please enter three integers: "); for (int i = 0; i < 3; i++) {
xxxxxx[x] = xxxxx.xxxxxxx();
xxx += xxxxxx[x];
xxxxxxx *= xxxxxx[x];
xxxxxxxx = xxxxxxxx < xxxxxx[x] ? xxxxxxxx : xxxxxx[x];
xxxxxxx = xxxxxxx > xxxxxx[x] ? xxxxxxx : xxxxxx[x];
}
Preview SimulateRollingDice.java
xxxxxx xxxx.xxxx.xxxxxx;
xxxxxx xxxxx.xxxxx.xxxxxxxxxxx;
xxxxxx xxxxx.xxxxx.xxxxxxxxx;
public class SimulateRollingDice {
public static void main(String args[]) { Random rand = new Random(System.currentTimeMillis()); int counter[] = new int[13];
String input = JOptionPane.showInputDialog(null,
“xxx xxxx xxxxx xxx xxxx xxxxxx xx xxxxxx?”, “xxxxx”,
xxxxxxxxxxx.xxxxxxxxxxx_xxxxxxx);
xxx x = xxxxxxx.xxxxxxxx(xxxxx);
xxx (xxx x = 0; x < x; x++) {
Price: $9
Buy Now
Checkout
Added to cart
Buy More Save More
Buy at least TWO items & save up to 30% OFF your ENTIRE order!
Rack up instant rebates in your shopping cart. Simply add items to your cart, and see the savings add up.
Discounts will automatically be applied on eligible orders.
COMP 274 Week 1 Lab – $9.00
COMP 274 Week 2 Lab Inheritance and Polymorphism – $12.00
COMP 274 Week 3 Lab Java application GUI – $15.00
COMP 274 Week 4 Lab Java Program to Convert Temperature – $15.00
COMP 274 Week 5 Lab Timer and Mouse Events and Java Graphics – $19.00
COMP 274 Week 6 Lab Calendar Program – $19.00
COMP 274 Week 7 Lab Java Doc – $19.00
Add to Cart
Checkout
Added to cart
FLASH SALE
$75
$108
Save $30
COMP274 Entire Course
Get Entire Course
You May Also Like:
COMP 274 Entire Course
COMP 274 Week 2 Lab Inheritance and Polymorphism
COMP 274 Week 3 Lab Java Application GUI
COMP 274 Week 4 Lab Java Program to Convert Temperature
COMP 274 Week 5 Lab Timer and Mouse Events and Java Graphics
COMP 274 Week 6 Lab Calendar Program
COMP 274 Week 7 Lab Java Doc
Only logged in customers who have purchased this product may leave a review.
Reviews
There are no reviews yet.