Chapter 4&5 Homework Answers
C H A P T E R 4
FUNDAMENTAL DATA TYPES
Home Work Answers
•• E4.4 Write a program that prompts the user for two integers and then prints
• The sum
• The difference
• The product
• The average
• The distance (absolute value of the difference)
• The maximum (the larger of the two)
• The minimum (the smaller of the two)
Hint: The max and min functions are declared in the Math class.
•• E4.5 Enhance the output of Exercise E4.4 so that the numbers are properly aligned:
Sum: 45
Difference: -5
Product: 500
Average: 22.50
Distance: 5
Maximum: 25
Minimum: 20
••• E4.13 Write a program that reads a number between 1,000 and 999,999 from the user, wherethe user enters a comma in the input. Then print the number without a comma.Here is a sample dialog; the user input is in color:Please enter an integer between 1,000 and 999,999: 23,45623456Hint: Read the input as a string. Measure the length of the string. Suppose it containsn characters. Then extract substrings consisting of the first n – 4 characters and the
C H A P T E R 5
DECISIONS
Home Work Answers
•• P5.6 Write a program that reads in the x- and y-coordinates of three corner points of a
triangle and prints out whether it has an obtuse angle, a right angle, or only acute
angles.
triangle and prints out whether it has an obtuse angle, a right angle, or only acute
angles.
Code :
E4.5
import
java.util.Scanner;
public
class Print {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter
First Number : ");
double first = in.nextDouble();
System.out.println("Please enter
Secend Number : ");
double secend = in.nextDouble();
System.out.println("The sum:
" + (first + secend));
System.out.println("The
difference: " + (first - secend));
System.out.println("The product:
" + (first * secend));
System.out.println("The average:
" + (first + secend) / 2);
System.out.println("The distance: "
+ Math.abs(first - secend));
System.out.println("The maximum:
" + Math.max(first, secend));
System.out.println("The minimum:
" + Math.min(first, secend));
}
}
E4.13
import
java.util.Scanner;
public
class Try {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter
an integer between 1,000 and 999,999: ");
String input = in.next();
int length = input.length();
String part1 =
input.substring(0,length-4);
String part2 =
input.substring(length-3,length);
String result = part1 + part2;
System.out.println(result);
}
}
P4.13
public
class Try {
public final double a = 17.27;
public final double b = 237.7;
public void f(double T, double RH) {
double f = ((a * T) / (b + T)) +
Math.log(RH);
double Td = (b * f) / (a - f);
System.out.printf("The dew point
temperature Td: %.4f°C.\n " , Td);
}
}
Test :
public
class TryTest {
public static void main(String[] args) {
Try ob = new Try();
ob.f(120, 0.6); } }
E5.7
public
class Try {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter Three
Number : ");
int first = in.nextInt();
int secend = in.nextInt();
int third = in.nextInt();
if (first >= secend &&
secend >= third || first <= secend && secend <= third )
System.out.println(" in
order");
else
System.out.println(" not in
order");
}
}
}
P5.6
import
java.util.Scanner;
public
class Triangle {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("enter x1 y1 x2
y2 x3 y3 : ");
double x1 = in.nextDouble();
double y1 = in.nextDouble();
double x2 = in.nextDouble();
double y2 = in.nextDouble();
double x3 = in.nextDouble();
double y3 = in.nextDouble();
double a =
Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2));
double b =
Math.sqrt(Math.pow((x2-x3),2)+Math.pow((y2-y3),2));
double c = Math.sqrt(Math.pow((x1-x3),2)+Math.pow((y1-y3),2));
if (
( (a*a)==(b*b)+(c*c) )
|| ( (b*b)==(a*a)+(c*c) )
|| ( (c*c)==(a*a)+(b*b) )
)
{System.out.println("Right
angle");}
else if ( ( (a*a)>(b*b)+(c*c) )
|| ( (b*b)>(a*a)+(b*b) )
|| ( (c*c)>(a*a)+(b*b) )
)
{System.out.println("Obtuse
angle");}
else
{System.out.println("Acute
angle");}
} }
End of Answers









Comments
Post a Comment