Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Lab 6 Using variables, Java expression evaluation with operations like +, -, *, %, / and order of operations Project 1. Using variables. Create a new Java project. Type the following lines (no need to...

1 answer below »
Lab 6
Using variables,
Java expression evaluation with operations like +, -, *, %, / and order of operations
Project 1. Using variables.
Create a new Java project. Type the following lines (no need to copy comments) in order as they appear, and answer questions from comments before moving further.
Code intentionally is not perfect, and if you run it as is, you will see e
or messages. Based on your previous teamwork assignment (#4) and lecture form last week you should be able to figure out why these e
or messages appear.
int minutes, seconds;
double seconds = 100;
minutes = 75;
System.out.println(minutes);
Now, uncomment line 3 with minutes = 75;
* Is your printing statement works now? So what was the e
or before?
Note that this is a comment on multi-lines!*
minutes = minutes+75;
System.out.println(minutes+seconds);
System.out.println(seconds);
seconds = 500;
System.out.println(seconds);
System.out.println(“minutes = ” + minutes);
*check font for double quotes above, sometimes it gets mixed up so it is better to type your own quotes*
Project 2. Two kinds of pluses in JAVA
* Plus sign marked in yellow is NOT regular alge
aic plus. It does not make sense to sum up text (whatever appears in double quotes is text) and number, right?
But it is useful for us to print some text and then immediately a number, this makes such number put into a context and more readable for users who do not see the code but only the output of the program they interact with. *
int hours = 2;
int minutes = 25;
int seconds = 55;
System.out.println(“hours = ” + hours);
concatenation
System.out.println(“minutes = ” + minutes);
concatenation
System.out.println(“seconds = ” + seconds);
concatenation
System.out.println(hours + minutes + seconds);
alge
aic plus
System.out.println(“total sum is: ” + (hours + minutes + seconds));
System.out.println(“total sum is: ” + hours + minutes + seconds));
Question: example above seem same as before except parenthesis. But the result is different! Why?
Project 3. Evaluating expressions
Java could be used as a calculator of sorts, but we need to be careful, as it does not always adhere to alge
aic rules. You have already see what happens with plus!
Java has more surprises for us!
Predict what the following statements will print, then try it in JAVA.
System.out.println(3+5);
System.out.println(3+5.0);
System.out.println(3*3);
System.out.println(3.0*3.0);
System.out.println(3/3);
System.out.println(3/3.0);
System.out.println(15.9/3);
System.out.println(1.5/5);

System.out.println(3/5);
careful! Integer division is special in JAVA
System.out.println(5/3);
careful! Integer division is special in JAVA
next is remainder operation!
System.out.println(3%2);
System.out.println(2%3);
System.out.println(3%3.0);
System.out.println(0%3.0);
multiple operations!
System.out.println(2 + 3%2);
System.out.println(1 - 2%3);
System.out.println(3 + 3*3.0);
System.out.println(3 - 0%3.0);
System.out.println(3 + 3*3%3.0);
System.out.println(3 – 3/3%3.0);
Question: which operations have higher order pf precedence? E.g. multiplication is “more important” than summation and should be done first. What about other operations?
Project 4 (optional, have extra 20 credits).
Advanced examples with pluses.
Predict what will be printed as a result of execution of each of the following printing statements.
Run each of these lines on its own (comment out others, or just type one by one).
System.out.println(11 + "xyz" + 22);
System.out.println XXXXXXXXXX + " xyz " + 33);
System.out.println("xyz " XXXXXXXXXX);
System.out.println(" xyz " + 11*22);
System.out.println(" xyz " -11+22);
Question: Figure out why your answers were not the same as JAVA answers. Check if it is consistent with rule you developed in previous Project. Update the rule if necessary (on how Java treats 2 kinds of pluses, how it can distinguish which is which).
Project 5 (optional, have extra 20 credits).
Advanced project on type compatibility.
int x = 2.5;
Question: results in e
or. Why?
double y = 2;
Question: not an e
or. Why? Try to print y value and see what happens!
Answered 1 days After Feb 19, 2021

Solution

Ankit answered on Feb 21 2021
143 Votes
Project/Project1.java
Project/Project1.java
public class Project1{
     public static void main(String []args){
        int minutes, seconds;
        
 double seconds = 100;
        seconds = 100;
        minutes = 75;
        System.out.println(minutes);
        /* Is your printing statement works now? So what was the e
or before? 
        Note that this is a comment on multi-lines!
        Ans. It was throwing e
or :- "variable minutes might not have been initialized" before because for using the variable or doing operations in it should be initialized with some values.
        Moreover we have to also comment line 4 because we can't define two data type for same variable ,for that we have to use "Type Conversion"
        *
        minutes = minutes+75;
        System.out.println(minutes+seconds); 
        System.out.println(seconds); 
        seconds = 500;
        System.out.println(seconds); 
        System.out.println("minutes = " + minutes);
     }
}
Project/Project1_E
or.PNG
Project/Project1_Screenshot.PNG
Project/Project2.java
Project/Project2.java
public class Project2{
     public static void main(String []args){
        int hours = 2;
        int minutes = 25;
        int seconds = 55;
        System.out.println("hours = " + hours);
concatenation
        System.out.println("minutes = " + minutes);
concatenation
        System.out.println("seconds = " + seconds);
concatenation
        System.out.println(hours + minutes + seconds); 
alge
aic plus
        System.out.println("total sum is: " + (hours + minutes + seconds));
        System.out.println("total sum is: " + hours + minutes + seconds);
        /*Question: example above seem same as before except parenthesis. But the result is different! Why?
        Ans. Because in Below most line we don't have use parenthesis and System.out.println considered it as a normal concatenation of values ,so each value of hours, minutes and seconds get printed after each other where as in above line there is parenthesis ,so the statement considered it as a one value within parenthesis and print it's alge
ic sum.That's is why sum is different*
     }
}
Project/Project2_Screenshot....
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here