0% found this document useful (0 votes)
5 views6 pages

Java Lossy Conversion Errors

The document contains the results of running multiple code sections that test variables, data types, and calculations in Java. Section 1 resulted in an error due to an uninitialized variable. Section 2 ran successfully and produced the expected output. Section 3 initialized and assigned a double variable. Section 4 demonstrated errors when assigning non-integer values to an integer variable without casting, but succeeded when casting was used. Section 5 showed an error due to an uninitialized constant, which was then fixed.

Uploaded by

JulioMartinez
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Java Lossy Conversion Errors

The document contains the results of running multiple code sections that test variables, data types, and calculations in Java. Section 1 resulted in an error due to an uninitialized variable. Section 2 ran successfully and produced the expected output. Section 3 initialized and assigned a double variable. Section 4 demonstrated errors when assigning non-integer values to an integer variable without casting, but succeeded when casting was used. Section 5 showed an error due to an uninitialized constant, which was then fixed.

Uploaded by

JulioMartinez
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab2

Julio Martinez

Section

Results:
run:
Exception in thread "main" [Link]: Uncompilable source
code - variable myInt might not have been initialized
at [Link]([Link])
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;
[Link]("myInt = " + myInt);
Got following response:
run:
Exception in thread "main" [Link]: Uncompilable source
code - incompatible types: possible lossy conversion from double to int
at [Link]([Link])
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)
Ran again with the following statement instead:
int myInt = (int)3.14;
[Link]("myInt = " + myInt);
Results:
run:
myInt = 3
BUILD SUCCESSFUL (total time: 0 seconds)

Section

Results/Error Message:
run:
Exception in thread "main" [Link]
Caused by: [Link]: Uncompilable source
code - variable numberOfStates not initialized in the default constructor
at lab2.Lab2.<clinit>([Link])
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([Link]);
[Link]("Enter 5 whole numbers for the scores");
[Link]("separated by by or more spaces: \n");
int score1, score2, score3, score4, score5;
score1 = [Link]();
score2 = [Link]();
score3 = [Link]();
score4 = [Link]();
score5 = [Link]();
int total = score1 + score2 + score3 + score4 + score5;
int count = 5;
double average = total/count;
double average2 = (double)total/count;
[Link]("average = " + average);
[Link]("average using doubel(total) = " + average2);

10

Source Code

package lab2;
/**
*
* @author julio martinez
*/
import [Link];
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;
[Link](myInt);
[Link](myDouble);
[Link](myChar);
*/
/** Part 2 */
int firstInt = 37;
int secondInt = 5;
[Link](firstInt+secondInt);
[Link](firstInt-secondInt);
[Link](firstInt*secondInt);
[Link](firstInt/secondInt);
[Link](firstInt%secondInt);

/** Part 3 */
double myDouble = 98.7;
myDouble = myDouble/7.5;
[Link]("myDouble = " + myDouble);

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


int myInt = 3.14;
[Link]("myInt = " + myInt); */
int myInt = (int)3.14;
[Link]("myInt = " + myInt);

/** Part 6
int myInt = 31+4*5-26/8%2;
[Link]("myInt = " +myInt); */
/** Part 7
double myDouble = 5.0;
myDouble = myDouble/14.3751;
myDouble = myDouble/3.14;
myDouble = myDouble*14.3751;
myDouble = myDouble*3.14;
[Link]("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;
[Link]("average = " + average);
[Link]("average using doubel(total) = " +average2);
*/
/** Part 9 */
Scanner keyboard = new Scanner([Link]);
[Link]("Enter 5 whole numbers for the scores");
[Link]("separated by by or more spaces: \n");
int score1, score2, score3, score4, score5;
score1 = [Link]();
score2 = [Link]();
score3 = [Link]();
score4 = [Link]();
score5 = [Link]();
int total = score1 + score2 + score3 + score4 + score5;
int count = 5;
double average = total/count;
double average2 = (double)total/count;
[Link]("average = " + average);
[Link]("average using doubel(total) = " + average2);

}
}

You might also like