Problem 1 is simple. You will create two variables, assign values to them using
ID: 668046 • Letter: P
Question
Problem 1 is simple. You will create two variables, assign values to them using Java statements ("hard coding" values), add them together, and display the sum.
Problem 1 (name this Lab1_Problem1)
This problem will help you gain experience using the Java environment, along with introducing the concept of variables, naming conventions, and simple arithmetic expressions. You will allocate memory to store two values, add them together, and output the results using the System.out.println(); method. There is no end-user input; you will be "hard coding" the values into your program.
Your initial development of the program will use int data types. After you get that working, then you will comment out the original code and write new Java statements using the data type double. This change will allow your final program to accommodate end-user data of real numbers.
When you are done, the graded program must work using input of data type double, showing your old int code in comments.
Step 1: Follow the instructions for creating a new Java project in Using DrJava.
Step 2: To help you memorize a basic Java program structure at this stage of your learning, type in the standard "public class..." and use Lab1_Problem1 for the name. Add the braces, then public static void main(String[] args);. Make sure you have the proper indentation in place (Allman or K&R).
Step 3: Run your program (Compile, then Run). Debug as needed. It won't do anything yet, but you'll be sure it's at an error-free baseline.
Step 4: Go back to Using DrJava and copy and paste the sections for the project's identification information and the other comments into your emerging program. The developer is you, the description something like, “Lab1_Problem1: simple hard-coded addition.”
Step 5: Add the code needed to declare two integer values. Name them iVal1 and iVal2. Use the "Other" section.
Step 6: Add the code needed to declare a third variable iSum which will be where you can store the result of your calculation. This should be done in the “Expression/Result Variables” section of DECLARATIONS.
Step 7: Add the code needed to assign actual numeric values to iVal1 and iVal2. Use the INITIALIZE VARIABLES section since you are assigning initial values to iVal1 and iVal2. There is NO end user input required; you are simply "hard coding" the values that will be used in the calculations.
Step 8: Add the code needed to calculate the sum of the two values stored in the variables you created—storing the result to a third variable. Use the PROCESSING AND CALCULATIONS section.
Step 9: Display the resulting values so your output looks like this (your actual results will vary depending on the values you choose). Use the OUTPUT section.
The sum of 3 and 4 is 7
Step 10: Test your application by changing the assigned values to non-integral values (i.e., values that have a fractional component to the right of the decimal point) like .26 and 15.54 for example.
If you tested your application by assigning non-integral values to your variables in the INITIALIZE VARIABLES section, you probably noticed your output wasn't what you expected. That's because precision was lost; the int data type can't handle fractional values. Hint: review your Notes 2, specifically the information on data types, and use data type double.
Step 11: To fix the precision problem, modify your data types to use double and make sure to rename your variables accordingly so the prefix matches—make sure you change every line in your program that uses the "old" int variable names and replace them with the new ones that can handle fractional values of type double. Comment out the lines that you replace.
Verify that the program will produce correct results with floating-point values such as the examples above (.26 and 15.54). Your final SAVED project must be based on the code AFTER you modify it to use data type double.
Step 12: When done, close DrJava and backup your files and folders.
Explanation / Answer
// Program1:
import java.io.*;
//Lab1_Problem1: simple hard-coded addition of integer values
class Lab1_Problem1
{
public static void main(String[] args)
{
//DECLARATIONS
int iVal1;
int iVal2;
int iSum;
//INITIALIZE VARIABLES
iVal1=3;
iVal2=4;
//Expression/Result Variables
//PROCESSING AND CALCULATIONS
iSum= iVal1+iVal2;
//OUTPUT
System.out.println("Sum of two integer values: "+ iSum);
}
}
//Output:
Welcome to DrJava. Working directory is C:UsersJavaprograms
> run Lab1_Problem1
Sum of two double values: 7
>
//Program2:
//Lab1_Problem1: simple hard-coded addition of integer values
/*
class Lab1_Problem1
{
public static void main(String[] args)
{
//DECLARATIONS
int iVal1;
int iVal2;
int iSum;
//INITIALIZE VARIABLES
iVal1=3;
iVal2=4;
//Expression/Result Variables
//PROCESSING AND CALCULATIONS
iSum= iVal1+iVal2;
//OUTPUT
System.out.println("Sum of two integer values: "+ iSum);
}
}
*/
//Lab1_Problem2: simple hard-coded addition of double values
import java.io.*;
class Lab1_Problem2
{
public static void main(String[] args)
{
//DECLARATIONS
// int iVal1;
double iVal1;
//int iVal2;
double iVal2;
//int iSum;
double iSum;
//INITIALIZE VARIABLES
//iVal1=3;
iVal1=.26;
iVal2=15.54;
//Expression/Result Variables
//PROCESSING AND CALCULATIONS
iSum= iVal1+iVal2;
//OUTPUT
System.out.println("Sum of two double values: "+ iSum);
}
}
//Output:
Welcome to DrJava. Working directory is C:UsersJavaprograms
> run Lab1_Problem2
Sum of two double values: 15.799999999999999
>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.