Package Title: Lab Questions Course Title: Big Java



Download 72.45 Kb.
Date16.08.2018
Size72.45 Kb.
Package Title: Lab Questions

Course Title: Big Java

Chapter Number: 4 Fundamental Data Types

Question type: Essay

1) Write a program that does the following: Create seven variables, one for each of the primitive number types in Java, and initialize each variable with any appropriate value. Print out the name of each variable and its value. Modify the value of each variable with an assignment statement and print out the names of the variables and their new values.

Next, create seven constants, one for each of the primitive number types in Java. Print the name of the constant and its value.

What happens if you try to assign a value to a constant?

2) Execute the program below. Each invocation of println outputs an arithmetic expression. The first two println commands are followed by comments that describe the operations that occur in each expression. Complete the program by adding a comment after each println statement that describes all the arithmetic operations that occur when evaluating the expression that is printed.
public class Expressions

{

public static void main(String[] args)



{

int a = 3;

int b = 4;

int c = 5;

int d = 17;

System.out.println((a + b)/ c);

// 3 and 4 are added with sum 7

// 7 is divided by 5 with quotient 1

System.out.println(a + b / c);

// 4 is divided by 5 with quotient 0

// 3 is added to 0 with sum 3

System.out.println(a++);

System.out.println(a--);

System.out.println(a + 1);

System.out.println(d % c);

System.out.println(d / c);

System.out.println(d % b);

System.out.println(d / b);

System.out.println(d + a / d + b);

System.out.println((d + a) / (d + b));

System.out.println(Math.sqrt(b));

System.out.println(Math.pow(a, b));

System.out.println(Math.abs(-a));

System.out.println(Math.max(a, b));

}

}
3) Write a program that prompts the user to enter two integers. Print the smaller of the two numbers entered. You’ll need to use a Scanner and a Math method.



4) Suppose you have 5 1/2 gallons of milk and want to store them in milk jars that can hold up to 0.75 gallons each. You want to know ahead of time how many completely filled jars you will have. The following program has been written for that purpose. What is wrong with it? Why? How can you fix it?

public class MilkJarCalculator

{

public static void main(String[] args)



{

double milk = 5.5; // gallons

double jarCapacity = 0.75; // gallons

int completelyFilledJars = milk / jarCapacity;

System.out.println(completelyFilledJars);

}

}



5.1) You want to know how many feet are in 3.5 yards, and how many inches are in 3.5 yards. You write the following program for that purpose:

public class DistanceConverter

{

public static void main(String[] args)



{

double yards = 3.5;

double feet = yards * 3;

double inches = feet * 12;

System.out.println(yards + "yards are" + feet + "feet");

System.out.println(yards + "yards are" + inches + "inches");

}

}
The problem with the program above is that using "magic numbers" makes it hard to maintain and debug. Modify the program so that it uses constants to improve legibility and make it easier to maintain.



5.2) Run the DistanceConverter program from Lab 4.5.1. What is the output? What change(s) would you make to the program to make the output more readable?

6) Adding (incrementing) or subtracting (decrementing) the value one from an integer variable is a common, everyday operation. To increment an int variable x, we could code
x = x + 1;
As an alternative, we could use the special operators ++ and -- to increment and decrement a variable. Use the first method to increment x in the program below. Print the value of x after incrementing. Use the ++ operator to increment y in the program below. Print the value of y after incrementing.

public class IncrementDemo

{

public static void main(String[] args)



{

int x = 10;

int y = -3;

// Put your code here

}

}

7.1) An annuity (sometimes called a reverse mortgage) is an account that yields a fixed payment every year until it is depleted. The present value of the annuity is the amount that you would need to invest at a given interest rate so that the payments can be made.



The present value of an annuity (PVann) at the time of the first deposit can be calculated using the following formula:

PVann = PMT · ({[(1 + i)n - 1 - 1] / i } / (1 + i)n - 1 + 1)

where:

PMT: periodic payment

i: periodic interest or compound rate

n: number of payments

What is the present value of an annuity that will pay out $10,000 in each of the next 20 years if the interest rate is 8 percent?

Write a program to calculate the present value of an annuity for these values. Remember that you can use Math.pow(x, y) to calculate xy.

What is your program?

7.2) Modify the program you created in Lab 4.7.1 so that the user can provide the values for pmt, i, and n through the console.

8.1) What is the output of the following program? Why?

public class AverageCalculator

{

public static void main(String[] args)



{

int age1 = 18;

int age2 = 35;

int age3 = 50;

int age4 = 44;
double averageAge = (age1 + age2 + age3 + age4) / 4;

System.out.println(averageAge);

}

}
8.2) Fix the program in Lab 4.8.1 so that it yields the correct result.



9.1) What is the output of the following program? Why?

public class PercentagePrinter

{

public static void main(String[] args)



{

double probability = 8.70;

int percentage = (int) (100 * probability);

System.out.println(percentage);



}

}

9.2) Fix the program from Lab 4.9.1 so that it displays the correct result. Remember that you can use Math.round to convert a floating-point value to its closest integer.



10) Using the substring method and concatenation, write a sequence of commands that will extract characters from inputString = "The quick brown fox jumps over the lazy dog" to make outputString = "Tempus fugit". Then print outputString.





© John Wiley & Sons, Inc. All rights reserved.


Download 72.45 Kb.

Share with your friends:




The database is protected by copyright ©sckool.org 2020
send message

    Main page