Lab Assignment 2 Solution

$30.00

Description

PURPOSE: This assignment familiarizes students with the use of variables, arithmetic, relational, and logical operators, and order of operations. It continues an introduction to basic software construction and the uses of output (using System.out.print(), Sytem.out.println(), and System.out.printf(), and input (using the Scanner class).

ASSIGNMENT: [15 pts.] Evaluating Expressions in Java

Instructions: For each of the expressions below, first attempt to solve the expression using your understanding of programming in Java. THEN write a simple computer program to verify if your intuition was correct. If your intuition was not correct then try to determine why the computer’s result is different than you expected. If you get stuck, ask a TA for help! Once you have solved one problem in this way THEN move on to the next problem.

Part A: [5 pts.] For each of the below expressions:

  1. Parenthesize the expression using the Java rules of operator precedence

  2. Determine whether the expression is syntactically valid (does not produce an error)

  1. Evaluate each expression and indicate the data type of the result. If the expression would produce an error, no result value need be given

int

double

char

String

data

x

y

a

b

p

s

expression

type

result

5

10

5.0

10.0

x % y – a + b

5

10

y = x = y / x * x

2

3

1.0

x / y * a

1

a’

p = (char)((int)p + x)

c’

s 240”

p + s

Part B: [5 pts.] Each of the following expressions is syntactically correct but does not produce the desired output. If possible, modify each expression by adding parentheses and type-casting so that it evaluates to the desired result. If the expression cannot be made to produce the correct output by these means, give a reason why.

int

double

char

String

data

desired

x

y

a

b

p

s

expression

type

result

4

2

5.0

15.0

y / x * a – b

double

-5.0

13

2

8’

x + p * y

int

42

3

7

8.0

1.0

b – a / x + y

double

0.2

0

1

a’

c”

p + x + p + y + s

string

abc”

Part C: [5 pts.] In the tables below you will find the variables p, q, r, and s. They are associated with integer values. You are to complete the table by entering a value at each the question marks (?) that are consistent with the result shown for the rest of that row. THEN write a simple computer program to verify if your intuition was correct. If your intuition was not correct then try to determine why the computer’s result is different than you expected.

var1

var2

var3

var4

Subexp1

Subexp2

Subexp3

Full expression

p

q

r

s

(p > q)

(r < s)

!(r < s)

(p > q) || !(r < s)

9

3

?

4

T

T

F

T

?

6

6

8

F

T

F

F

2

?

5

3

T

F

T

T

1

2

8

?

F

F

T

T

var1

var2

var3

var4

Subexp1

Subexp2

Full expression

p

q

r

s

(p ≤ q)

(r ≥ s)

(p ≤ q) && (r ≥ s)

6

5

3

?

F

T

F

2

3

?

5

T

T

T

6

?

5

3

T

T

T

?

2

3

2

F

T

F

BEFORE MOVING ON: SHOW YOUR FINAL RESULTS TO YOUR TA IN CLASS FOR GRADING

ASSIGNMENT: [5 pts.] Tax Calculator

Consider the calculation of income tax using system such as the US marginal rate tax brackets. The rate of tax on income varies based upon total taxable income. For example, a taxable income of $20,000 would pay a rate of 10% on the first $9,225 of income, but would then pay a rate of 15% on each dollar of taxable income over 9,225 (until reaching the next bracket at $37,450). For $20,000, this would result in a tax of $9,225 * 10% + $10,775 * 15% = $922.50 + $1,616.25 = $2538.75. Thus the ACTUAL tax rate paid on an income of $20,000 is $2538.75 / $20,000 = 12.69%.

Your task is to construct a program that accepts, as input, a decimal number representing an ordinary taxable income. Your program is to determine the amount of tax paid on that income (using the tax brackets above) AND to determine the actual tax rate paid on the income as a whole. You may assume that the user will enter a valid input (a non-negative decimal number appropriate for storage as in a double variable).

Sample input and output for the Tax Calculator:

2015 Tax calculator

income in dollars: 500

Please enter the taxable

The tax on $500.00 is $50.00

Actual paid tax rate is:

10.00 percent

2015 Tax calculator

income in dollars: 20000

Please enter the taxable

The tax on $20,000.00 is

$2538.75

Actual paid tax rate is:

12.69 percent

2015 Tax calculator

income in dollars: 150000

Please enter the taxable

The tax on $150,000.00 is $35071.25

Actual paid tax rate is:

23.38 percent

2015 Tax calculator

income in dollars: 500000

Please enter the taxable

The tax on $500,000.00 is $154,369.05

Actual paid tax rate is:

30.87 percent

Show your program and output to your Lab TA. Be prepared to answer questions regarding your program and to receive feedback regarding variable names and other stylistic choices.

Grading Rubric

1 – Calculates the correct taxable amount for at least some valid taxable amounts

1 – Calculates the correct taxable amount for any taxable income

1 – Calculates the correct actual paid tax rate

1 – Output is correctly formatted (two decimal places, commas in appropriate places values; e.g. $12,020.10)

1 – Documentation (comments) and programming style (variable names, ease of readability, following style guidelines).

Save your lab 2 assignment by creating a zip file. Show your lab assignments to your TA. Upload your lab assignments to the Pilot drop box before the lab due date. Ask your TA for help if you have any questions.


error: Content is protected !!