Vous êtes sur la page 1sur 9

R5.2b.

Find the errors in the following if statements:


if (x = 1) y++; else if (x = 2) y = y + 2;
The relational operator equal is not =, the relational operator equal is ==
R5.4b.
Find the errors in the following if statements:
if (1 <= x <= 10)
System.out.println(x);
The correct syntax for the expression

is ((x <= 10) && (x >= 1)).

R5.4d.
Find the errors in the following if statements:
if (input.equalsIgnoreCase("N") || "NO")
return;
NO is not a valid Boolean expression.
May be the correct expression is:
if (input.equalsIgnoreCase("N") || input.equalsIgnoreCase("NO"))
return;
R5.11.
Give a set of four test cases for the algorithm of Exercise R5.10 that covers all
branches.
1.
if((letter=='a')||(letter=='c')||(letter=='e')||(letter=='g')){
if((number % 2) == 1){
color = "black";
}else{
color="white";
}
}else{
if((number % 2)==0){
color="black";
}else
color="white";
}

2.
if((letter=='a')||(letter=='c')||(letter=='e')||(letter=='g')){
color= ((number % 2) == 1) ? "black" : "white";
}else{
color= ((number % 2) == 0) ? "black" : "white";
}

3.
color= ((number % 2) == 1) ? "black" : "white";
4.
color= ((number % 2) != 1) ? "white" : "black";

R5.14.
True or false? A && B is the same as B && A for any Boolean conditions A and B.
When & is used it will evaluate both the expressions regardless of the fact that it
finds first expression as FALSE and only then will it give an answer.
Whereas if && was used in place of &, after it had evaluated first expression and had
found result of first expression as FALSE, it would not have evaluated second
expression. Thus saving time.
FALSE.

R5.19.
Explain the difference between the tests
r == s
and
r.equals(s)
where both r and s are of type Rectangle.
If you compare two object references with the == operator, you test whether the
references refer to the same object. Here is an example:
Rectangle r = new Rectangle(5, 10, 20, 30);
Rectangle s = r;
Rectangle t = new Rectangle(5, 10, 20, 30);
The comparison
r == s
is true. Both object variables refer to the same object. But the comparison
r == t
is false. The two object variables refer to different objects. It does not matter that
the objects have identical contents.
You can use the equals method to test whether two rectangles have the same contents,
that is, whether they have the same upper-left corner and the same width and height.
For example, the test r.equals(t) is true.
R5.26.
Give a set of test cases for the earthquake program in Section 5.3.1. Ensure coverage
of all branches.
public static void main(String[] args)
{
Earthquake quake1 = new Earthquake(9.0);
System.out.println(quake1.getDescription()=="Most structures fall" ? Pass:Fail);

Earthquake quake1 = new Earthquake(7.5);


System.out.println(quake1.getDescription()=="Many buildings destroyed" ?
Pass:Fail);
Earthquake quake1 = new Earthquake(6.5);
System.out.println(quake1.getDescription()=="Many buildings considerably damaged,
some collapse" ? Pass:Fail);
Earthquake quake1 = new Earthquake(5.5);
System.out.println(quake1.getDescription()=="Damage to poorly constructed buildings" ?
Pass:Fail);
Earthquake quake1 = new Earthquake(4.0);
System.out.println(quake1.getDescription()=="Felt by many people, no destruction" ?
Pass:Fail);
Earthquake quake1 = new Earthquake(1.0);
System.out.println(quake1.getDescription()=="Generally not felt by people" ?
Pass:Fail);
Earthquake quake1 = new Earthquake(-1.0);
System.out.println(quake1.getDescription()=="Negative numbers are not valid" ?
Pass:Fail);
}

P5.4.
Write a program that translates a letter grade into a number grade. Letter grades are
A B C D F, possibly followed by + or -. Their numeric values are 4, 3, 2, 1, and 0.
There is no F+ or F-. A + increases the numeric value by 0.3, a - decreases it by 0.3.
However, an A+ has the value 4.0. All other inputs have value 1.
Use a class Grade with a method getNumericGrade.
public class Grade {
double getNumericGrade(String letterGrade){
double numericGrade=0.0;
if((letterGrade=="A") || (letterGrade=="A+")) numericGrade = 4.0; else
if(letterGrade=="A-") numericGrade = 3.7; else
if(letterGrade=="B+") numericGrade = 3.3; else
if(letterGrade=="B") numericGrade = 3.0; else
if(letterGrade=="B-") numericGrade = 2.7; else
if(letterGrade=="C+") numericGrade = 2.3; else
if(letterGrade=="C") numericGrade = 2.0; else
if(letterGrade=="C-") numericGrade = 1.7; else
if(letterGrade=="D+") numericGrade = 1.3; else
if(letterGrade=="D") numericGrade = 1.0; else
if(letterGrade=="D-") numericGrade = 0.7; else
if(letterGrade=="F") numericGrade = 0.0; else
numericGrade=-1;
return numericGrade;
}
}

P5.8.
The original U.S. income tax of 1913 was quite simple. The tax was
1 percent on the rst $50,000.
2 percent on the amount over $50,000 up to $75,000.
3 percent on the amount over $75,000 up to $100,000.
4 percent on the amount over $100,000 up to $250,000.
5 percent on the amount over $250,000 up to $500,000.
6 percent on the amount over $500,000.
There was no separate schedule for single or married taxpayers. Write a program
that computes the income tax according to this schedule.
import java.util.Scanner;
public class TaxCalculator
{
private double income=0.0;
public TaxCalculator(double income){this.income=income;}
public double getTax()
{
double Tax=0.0;
if(income<=50000) Tax = income*0.01; else
if(income<=75000) Tax = income*0.02; else
if(income<=100000) Tax = income*0.03; else
if(income<=250000) Tax = income*0.04; else
if(income<=500000) Tax = income*0.05; else
Tax = income*0.06;
return Tax;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Please enter your income: ");
double income = in.nextDouble();
TaxCalculator T = new TaxCalculator(income);
System.out.println("Tax: "
}
}

+ T.getTax());

P5.12.
A year with 366 days is called a leap year. A year is a leap year if it is divisible
by 4 (for example, 1980). However, since the introduction of the Gregorian calendar on
October 15, 1582, a year is not a leap year if it is divisible by 100 (for example,
1900); however, it is a leap year if it is divisible by 400 (for example, 2000). Write
a program that asks the user for a year and computes whether that year is a leap year.
Implement a class Year with a predicate method boolean isLeapYear().
import java.util.Scanner;
public class LeapYear
{
private int year=0;
public LeapYear(int year){this.year=year;}
public boolean isLeapYear()
{
return (year%4==0 || (year%400==0 && year%100!=0));
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Please enter a year: ");
double year = in.nextInteger();
LeapYear T = new LeapYear(year);
System.out.println("Is leap: "
}
}

+ T.isLeapYear());

P5.17.
Write a unit conversion program that asks users to identify the
unit from which they want to convert and the unit to which they want to convert.
Legal units are in, ft, mi, mm, cm, m, and km. Declare two objects of a class
UnitConverter that convert between meters and a given unit.
Convert from:
in
Convert to:
mm
Value:
10
10 in = 254 mm
import java.util.Scanner;

public class UnitConverter {


private String from="m";
private String to="cm";
double amount = 0.0;
UnitConverter(String from, String to, double amount){
this.amount=amount; this.from=from; this.to=to;
}
public double Convert(){
double convert=0.0;
double meterFactor=1;
meter
double inchFactor=39.37007874015748031496062992126;
meter
double feetFactor=3.2786885245901639344262295081967;
meter
double mileFactor=6.2137119223733396961743418436332e-4;
meter
double milliFactor=1000;
meter
double centFactor=100;
meter;
double kiloFactor=1e-3;
meter;
double fromFactor=1;
double toFactor=1;
if(from.equals("m")) fromFactor=meterFactor;
if(from.equals("in"))fromFactor=inchFactor;
if(from.equals("ft"))fromFactor=feetFactor;
if(from.equals("mi"))fromFactor=mileFactor;
if(from.equals("mm"))fromFactor=milliFactor;
if(from.equals("cm"))fromFactor=centFactor;
if(from.equals("km"))fromFactor=kiloFactor;
if(to.equals("m")) toFactor=meterFactor;
if(to.equals("in"))toFactor=inchFactor;
if(to.equals("ft"))toFactor=feetFactor;
if(to.equals("mi"))toFactor=mileFactor;

//meters

in 1

//inches

in 1

//foots

in 1

//miles

in 1

//millimeters in 1
//centimeters in a
//kilometers

in 1

if(to.equals("mm"))toFactor=milliFactor;
if(to.equals("cm"))toFactor=centFactor;
if(to.equals("km"))toFactor=kiloFactor;
convert=amount*toFactor/fromFactor;

return convert;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.print("Please enter your amount in meters: ");
double amount = in.nextDouble();
System.out.print("Please enter your target unit: ");
String unit = in.next();
UnitConverter U = new UnitConverter("m",unit,amount);
System.out.println("Is equivalent to " + U.Convert() + " " + unit);
}
}

P5.18.
P5.18 A line in the plane can be specied in various ways:
by giving a point (x, y) and a slope m
by giving two points (x1, y1), (x2, y2)
as an equation in slope-intercept form y = mx + b
as an equation x = a if the line is vertical
Implement a class Line with four constructors, corresponding to the four cases
above. Implement methods
boolean intersects(Line other)
boolean equals(Line other)
boolean isParallel(Line other)

public class Line {


private double m=0;
private double interceptY=0;
private double interceptX=0;
private boolean infScope=false;
public Line(double x,double y, double m){
//
y0-y1
y0-y1
// m=------- ==> y0 = m.(x0-x1) + y1 ==> x0 = -------- + x1
//
x0-x1
m
this.m=m;
this.interceptY=this.m*(0-x)+y;
this.infScope=false;
this.interceptX=(0-y)/m + x;
};

public Line(double x1,double y1, double x2, double y2){


//
y0-y1
// m=------- ==> y0 = m.(x0-x1) + y1
//
x0-x1
this.infScope=(x1==x2);
if(!this.infScope){
this.m=(y1-y2)/(x1-x2);
this.interceptY=this.m*(0-x1)+y1;
this.interceptX=(0-y1)/m + x1;
}
};
public Line(double m,double interceptY){
//
y0-y1
// m=------- ==> y0 = m.(x0-x1) + y1
//
x0-x1
// y = m x + b

==> x = (y/m) - b/m

this.infScope=false;
this.m=0;
this.interceptY=interceptY;
this.interceptX=-this.interceptY/this.m;
};
public Line(double a){
this.m=0;
this.interceptY=0;
this.interceptX=a;
this.infScope=true;
};
public
public
public
public

boolean getInfScope(){return infScope;};


double getm(){return m;};
double getInterceptY(){return interceptY;};
double getInterceptX(){return interceptX;};

public boolean intersects(Line l){


return (this.getm()!=l.getm());
}
public boolean equals(Line l){
boolean e=false;
if(this.getInfScope() && !l.getInfScope() || !this.getInfScope() &&
l.getInfScope()){e=false;}else
if(!this.getInfScope()){e=this.getm()==l.getm() &&
this.getInterceptY()==l.getInterceptY();}else
e=this.getInterceptX()==l.getInterceptX();
return e;
}
public boolean isParallel(Line l){
return this.getInfScope() && l.getInfScope() || this.getm() == l.getm();
};
}