Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a command - line program that accepts two numbers and a string representing an arithmetic operation: plus, minus, times, and div in either upper

Write a command-line program that accepts two numbers and
a string representing an arithmetic operation: "plus", "minus", "times", and
"div" in either upper or lower case and evaluates the resulting expression.
Examples of running the program (source in Calc.java) several times:
74 Chapter 9 Immutable Objects
> java Calc
Usage: number plus|minus|times|div number
> java Calc 3 plus 4.5
7.5
> java Calc 7.5 div 2
3.75
> java Calc 6 MINUS 1.25
4.75
The point of this exercise is to make sure you understand the mechanism for passing objects as parameters.
For the following program, draw a stack diagram showing the local variables and parameters of main and riddle just before riddle returns. Use arrows to show which objects each variable references.
What is the output of the program?
Is the blank object mutable or immutable? How can you tell?
public static int riddle(int x, Point p){
x = x +7;
return x + p.x + p.y;
}
public static void main(String[] args){
int x =5;
Point blank = new Point(1,2);
System.out.println(riddle(x, blank));
System.out.println(x);
System.out.println(blank.x);
System.out.println(blank.y);
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions