0% found this document useful (0 votes)
2 views

Java_CH8_Solutions

Uploaded by

wwangyibo17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java_CH8_Solutions

Uploaded by

wwangyibo17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Chapter 8

1. (Rectangle Class) Create a class Rectangle with attributes length and width, each of which
defaults to 1. Provide methods that calculate the rectangle’s perimeter and area. It has set and get
methods for both length and width. Theset methods should verify that length and width are each
floating-point numbers larger than 0.0 and less than 20.0. Write a program to test class Rectangle.
Solution:
public class Rectangle {
private double length;
private double width;

// constructor
public Rectangle(){
setLength(1.0f);
setWidth(1.0f);
}
public Rectangle(double length, double width){
setLength(length);
setWidth(width);
}
public void setLength(double length){
if(length >= 0.0f && length <=20.0f)
this.length = length;
else
throw new IllegalArgumentException("length must be between 0.0 and 20.0");
}
public void setWidth(double width){
if(width >= 0 && width <= 20)
this.width = width;
else
throw new IllegalArgumentException("width must be between 0.0 and 20.0");
}
public double getLength(){
return this.length;
}
public double getWidth(){
return this.width;
}
// calculate perimeter
public double getPerimeter(){
return (length * 2) + (width * 2);
}
// calculate area
public double getArea(){
return length * width;
}//end getArea

// convert length, width, area and perimeter to string


1
public String toString(){
String s = String.format("Width: %.2f\nLength: %.2f\nArea: %.2f\nPerimeter: %.2f\
n",getLength(), getWidth(), getArea(), getPerimeter());

return s;
}//end toString
}//end class

public class RectangleTest {

public static void main(String[] args) {

System.out.println("rect1 default constructor");


Rectangle rect1 = new Rectangle();
System.out.printf("%s\n", rect1.toString());

System.out.println("rect2 constructed with width and length");


Rectangle rect2 = new Rectangle(4.0f, 3.0f);
rect2.setLength(2.0f);
rect2.setWidth(8.0f);
System.out.printf("%s\n", rect2.toString());

// constructor with invalid values


try{
Rectangle rect3 = new Rectangle(40.0f, -1.0f);
}catch(IllegalArgumentException e){
System.out.printf("\nException while initialising rect3: %s\
n",e.getMessage());
}
}

2. Write an enum type TrafficLight, whose constants (RED, GREEN, YELLOW) take one parameter
—the durationof the light. Write aprogram to test the TrafficLight enum sothat it displaysthe enum
constants and their durations.
Solution:
public enum TrafficLight{
RED(24),
GREEN(40),
YELLOW(5);

// instance field
private final int duration;

// constructor
TrafficLight(int duration){
2
this.duration = duration;
}
// accessor for duration
public int getDuration(){
return this.duration;
}
}//end class

public class TrafficLightTest{


public static void main(String[] args){
for(TrafficLight tf : TrafficLight.values()){
System.out.printf("%s - %d\n", tf, tf.getDuration());
}//end for loop
}//end main
}//end class

3. Write a program to display a date with two formats: DD/MM/YYYY and Month Name
DD/YYYY. Create a Date class with a constructor that has three parameters: day, month and year.
The program should check for the invalid values of day, month and year when initializing the date.

Solution:

package CH_8;

public class Date{

private static int[] daysPerMonth =


{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

private static final String[] strMonths =


{"", "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};

private int month;


private int day;
private int year;

public Date(int day,int month,int year){


this.year = year;
CheckLeapYear();

if(month < 1 || month > 12) {


throw new IllegalArgumentException("invalid month");
}
this.month = month;
if(day < 1 || day > daysPerMonth[month]) {
throw new IllegalArgumentException("invalid day");
3
}
this.day = day;

public void CheckLeapYear() {


if(this.year % 4 == 0) {
daysPerMonth[2] = 29;
}else {
daysPerMonth[2] = 28;
}
}
public void DayMonthYear() {
System.out.printf("%02d/%02d/%03d",month,day,year);
}
public void DayMonthNameYear() {
System.out.printf("%7s %02d/%03d",strMonths[month],day,year);
}

//DateTest.java
import java.util.Scanner;
public class DateTest{
public static void main(String[] args){
Scanner input = new Scanner(System.in);

System.out.print("Enter Day: ");


int day = input.nextInt();
System.out.print("Enter Month: ");
int month = input.nextInt();
System.out.print("Enter Year: ");
int year = input.nextInt();

Date date = new Date(day,month,year);


date.displayDate();

}
}

Output:

Date: 06/26/1998
Date with month name: June 26/1998

You might also like