1 Section: Julio Martinez

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Lab2

Julio Martinez

Section

Results:
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source
code - variable myInt might not have been initialized
at lab2.Lab2.main(Lab2.java:15)
Java Result: 1
BUILD SUCCESSFUL (total time: 8 seconds)

Section

My guess for results are:


42
32
185
7
2
Results:
run:
42
32
185
7
2
BUILD SUCCESSFUL (total time: 0 seconds)

Section

Results:
1

run:
myDouble = 13.16
BUILD SUCCESSFUL (total time: 0 seconds)

Section

Ran the following statement:


int myInt = 3.14;
System.out.println("myInt = " + myInt);
Got following response:
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source
code - incompatible types: possible lossy conversion from double to int
at lab2.Lab2.main(Lab2.java:29)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)
Ran again with the following statement instead:
int myInt = (int)3.14;
System.out.println("myInt = " + myInt);
Results:
run:
myInt = 3
BUILD SUCCESSFUL (total time: 0 seconds)

Section

Results/Error Message:
run:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source
code - variable numberOfStates not initialized in the default constructor
at lab2.Lab2.<clinit>(Lab2.java:8)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
2

Can correct the problem by initializing the constant at time of declaration as shown below:
public static final int numberOfStates = 50;

Section

I think the value is going to be 50


Results:
run:
myInt = 50
BUILD SUCCESSFUL (total time: 0 seconds)

Section

Result:
run:
myDouble = 1.0000000000000002
BUILD SUCCESSFUL (total time: 0 seconds)

Section

Statements using extra variable identified by average 2 instead of changing


line:
double average = total/count;
double average2 = (double)total/count;
Results:
run:
average = 87.0
average using doubel(total) = 87.6
BUILD SUCCESSFUL (total time: 0 seconds)

Section

Rewritten Code/Statements for user input:


Scanner keyboard = new Scanner(System.in);
System.out.print("Enter 5 whole numbers for the scores");
System.out.print("separated by by or more spaces: \n");
int score1, score2, score3, score4, score5;
score1 = keyboard.nextInt();
score2 = keyboard.nextInt();
score3 = keyboard.nextInt();
score4 = keyboard.nextInt();
score5 = keyboard.nextInt();
int total = score1 + score2 + score3 + score4 + score5;
int count = 5;
double average = total/count;
double average2 = (double)total/count;
System.out.println("average = " + average);
System.out.println("average using doubel(total) = " + average2);

10

Source Code

package lab2;
/**
*
* @author julio martinez
*/
import java.util.Scanner;
public class Lab2 {
/* Part 5 (Error Results with Commented Code)
public static final int numberOfStates
numberofStates = 50; */
public static final int numberOfStates = 50;
public static void main(String[] args) {
/** Part 1
int myInt;
double myDouble;
4

char myChar;
System.out.println(myInt);
System.out.println(myDouble);
System.out.println(myChar);
*/
/** Part 2 */
int firstInt = 37;
int secondInt = 5;
System.out.println(firstInt+secondInt);
System.out.println(firstInt-secondInt);
System.out.println(firstInt*secondInt);
System.out.println(firstInt/secondInt);
System.out.println(firstInt%secondInt);

/** Part 3 */
double myDouble = 98.7;
myDouble = myDouble/7.5;
System.out.println("myDouble = " + myDouble);

/** Part 4 First (Error Results with Commented Code)


int myInt = 3.14;
System.out.println("myInt = " + myInt); */
int myInt = (int)3.14;
System.out.println("myInt = " + myInt);

/** Part 6
int myInt = 31+4*5-26/8%2;
System.out.println("myInt = " +myInt); */
/** Part 7
double myDouble = 5.0;
myDouble = myDouble/14.3751;
myDouble = myDouble/3.14;
myDouble = myDouble*14.3751;
myDouble = myDouble*3.14;
System.out.println("myDouble = " + myDouble);
5

*/
/** Part 8
int score1 = 72, score2 = 81, score3 = 88, score4 = 98, score5 = 99;
int total = score1 + score2 + score3 + score4 + score5;
int count = 5;
double average = total/count;
double average2 = (double)total/count;
System.out.println("average = " + average);
System.out.println("average using doubel(total) = " +average2);
*/
/** Part 9 */
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter 5 whole numbers for the scores");
System.out.print("separated by by or more spaces: \n");
int score1, score2, score3, score4, score5;
score1 = keyboard.nextInt();
score2 = keyboard.nextInt();
score3 = keyboard.nextInt();
score4 = keyboard.nextInt();
score5 = keyboard.nextInt();
int total = score1 + score2 + score3 + score4 + score5;
int count = 5;
double average = total/count;
double average2 = (double)total/count;
System.out.println("average = " + average);
System.out.println("average using doubel(total) = " + average2);

}
}

You might also like