What value would b have after each of the statements that follow is executed? Assume that m, k, j are reinitialized before each statement. It may help to parenthesize the right-hand side of the statements before evaluating them.
For each of the following expressions, if it is valid, determine the value of the variable on the left-hand side (if not, change it to a valid expression):
Each of the problems that follow asks you to write a method. Of course, as you are developing the method in a stepwise fashion, you should test it. Hereβs a simple application program that you can use for this purpose:
public class MethodTester {
public static int square(int n) {
return n * n;
}
public static void main(String args[]) {
System.out.println("5 squared = " + square(5));
}
}
Just replace the square() method with your method. Note that you must declare your method static if you want to call it directly from main() as we do here.
Write a method to calculate the sales tax for a sale item. The method should take two double parameters, one for the sales price and the other for the tax rate. It should return a double. For example, calcTax(20.0, 0.05) should return 1.0.
Challenge: Suppose youβre writing a program that tells what day of the week someoneβs birthday falls on this year. Write a method that takes an int parameter, representing what day of the year it is, and returns a String like ``Monday.ββ For example, for 2004, a leap year, the first day of the year was on Thursday. The thirty-second day of the year (February 1, 2004) was a Sunday, so getDayOfWeek(1) should return βThursdayβ and getDayOfWeek(32) should return βSunday.β Hint.
Challenge: As part of the birthday program, youβll want a method that takes the month and the day as parameters and returns what day of the year it is. For example, getDay(1,1) should return 1; getDay(2,1) should return 32; and getDay(12,31) should return 365. Hint.
Write a Java method that converts a char to lowercase. For example, toLowerCase('A') should return `aβ. Make sure you guard against method calls like toLowerCase('a').
Challenge: Write a Java method that shifts a char by n places in the alphabet, wrapping around to the start of the alphabet, if necessary. For example, shift('a',2) should return `cβ; shift('y',2) should return `aβ. This method can be used to create a Caesar cipher, in which every letter in a message is shifted by n places---hfu ju? (Refer to Chapter~1 exercises for a refresher on Caesar cipher.)
Write a Java application that first prompts the user for three numbers, which represent the sides of a rectangular cube, and then computes and outputs the volume and the surface area of the cube.
Design and write a Java GUI that converts kilometers to miles and vice versa. Use a JTextField for I/O and JButtons for the various conversion actions.
Design and write a GUI that allows a user to calculate the maturity value of a CD. The user should enter the principal, interest rate, and years, and the applet should then display the maturity value. Make use of the BankCD class covered in this chapter. Use separate JTextFields for the userβs inputs and a separate JTextField for the result.
Design and write a GUI that lets the user input a birth date (month and day) and reports what day of the week it falls on. Use the getDayOfWeek() and getDay() methods that you developed in previous exercises.
Design and write a GUI that allows the users to input their exam grades for a course and computes their average and probable letter grade. The applet should contain a single JTextField for inputting a grade and a single JTextField for displaying the average and letter grade. The program should keep track internally of how many grades the student has entered. Each time a new grade is entered, it should display the current average and probable letter grade.
One of the reviewers of this text has suggested an alternative design for the Temperature class (FigureΒ 5.7.6). According to this design, the class would contain an instance variable, say, temperature, and access methods that operate on it. The access methods would be:
One way to implement this design is to store the temperature in the Kelvin scale and then convert from and to Kelvin in the access methods. The formula for converting Kelvin to Celsius is
Draw a UML class diagram representing this design of the Temperature class. Which design is more object oriented, this one or the one used in FigureΒ 5.7.6?