Skip to content

Commit 46001a2

Browse files
committed
Update solutions
1 parent f59fd87 commit 46001a2

6 files changed

+209
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* (Occurrence of max numbers) Write a program that reads integers, finds the largest
3+
* of them, and counts its occurrences. Assume the input ends with number 0. Suppose
4+
* you entered 3 5 2 5 5 5 0; the program finds that the largest is 5 and the
5+
* occurrence count for 5 is 4.
6+
* (Hint: Maintain two variables, max and count. max stores the current max number
7+
* and count stores its occurrences. Initially, assign the first number to max and 1 to
8+
* count. Compare each subsequent number with max. If the number is greater than
9+
* max, assign it to max and reset count to 1. If the number is equal to max, increment
10+
* count by 1.)
11+
*
12+
* Enter numbers: 3 5 2 5 5 5 0
13+
* The largest number is 5
14+
* The occurrence count of the largest number is 4
15+
*
16+
* Created by Sven on 06/05/19.
17+
*/
18+
package Chapter05;
19+
20+
import java.util.Scanner;
21+
22+
public class Exercise0541_Occurrence_of_max_numbers {
23+
public static void main(String[] args) {
24+
Scanner input = new Scanner(System.in);
25+
System.out.print("Enter numbers: ");
26+
27+
int next = input.nextInt();
28+
int max = next;
29+
int count = 1;
30+
31+
while (next != 0) {
32+
next = input.nextInt();
33+
if (next > max) {
34+
max = next;
35+
count = 1;
36+
} else if (next == max) {
37+
count++;
38+
}
39+
}
40+
41+
System.out.println("The largest number is " + max);
42+
System.out.println("The occurrence count of the largest number is " + count);
43+
}
44+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* (Financial application: find the sales amount) Rewrite Programming Exercise 5.39
3+
* as follows:
4+
* ■ Use a while loop instead of a do-while loop.
5+
* ■ Let the user enter COMMISSION_SOUGHT instead of fixing it as a constant.
6+
*
7+
* Created by Sven on 06/05/19.
8+
*/
9+
package Chapter05;
10+
11+
import java.util.Scanner;
12+
13+
public class Exercise0542_Financial_application_find_the_sales_amount {
14+
public static void main(String[] args) {
15+
Scanner input = new Scanner(System.in);
16+
17+
double commission = 0.0;
18+
double salesAmount = 0.0;
19+
20+
System.out.print("Enter commission amount: ");
21+
double COMMISSION_SOUGHT = input.nextInt();
22+
23+
while (salesAmount - commission < COMMISSION_SOUGHT) {
24+
salesAmount += 0.01;
25+
26+
if (salesAmount > 10000.0) {
27+
commission = 700.0 + (salesAmount - 10000.0) * 0.1;
28+
} else if (salesAmount > 5000.0) {
29+
commission = 300.0 + (salesAmount - 5000.0) * 0.08;
30+
} else {
31+
commission = salesAmount * 0.06;
32+
}
33+
}
34+
35+
System.out.println("You need $" + (int) salesAmount + " sales amount to make a commission of $" + COMMISSION_SOUGHT);
36+
}
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* (Math: combinations) Write a program that displays all possible combinations
3+
* for picking two numbers from integers 1 to 7. Also display the total number of
4+
* all combinations.
5+
*
6+
* 1 2
7+
* 1 3
8+
* ...
9+
* ...
10+
* The total number of all combinations is 21
11+
*
12+
* Created by Sven on 06/05/19.
13+
*/
14+
package Chapter05;
15+
16+
public class Exercise0543_Math_combinations {
17+
public static void main(String[] args) {
18+
int count = 0;
19+
for (int i = 1; i < 8; i++) {
20+
for (int j = i + 1; j < 8; j++) {
21+
System.out.println(i + " " + j);
22+
count++;
23+
}
24+
}
25+
System.out.println("The total number of all combinations is " + count);
26+
}
27+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* (Computer architecture: bit-level operations) A byte value is stored in 8 bits. Write
3+
* a program that prompts the user to enter a byte integer and displays the 8 bits for
4+
* the integer. Here are sample runs:
5+
*
6+
* Enter an integer: 5
7+
* The 8 bits are 00000101
8+
*
9+
* Enter an integer: –5
10+
* The 8 bits are 11111011
11+
*
12+
* (Hint: You need to use the bitwise right shift operator (>>) and the bitwise AND
13+
* operator (&), which are covered in Appendix G, Bitwise Operations.)
14+
*
15+
* Created by Sven on 06/05/19.
16+
*/
17+
package Chapter05;
18+
19+
import java.util.Scanner;
20+
21+
public class Exercise0544_Computer_architecture_bit_level_operations {
22+
public static void main(String[] args) {
23+
Scanner input = new Scanner(System.in);
24+
System.out.print("Enter an integer: ");
25+
int number = input.nextInt();
26+
27+
System.out.print("he 8 bits are ");
28+
for (int i = 7; i >= 0; i--) {
29+
System.out.print((number >> i) & 1);
30+
}
31+
}
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* (Statistics: compute mean and standard deviation) In business applications, you
3+
* are often asked to compute the mean and standard deviation of data. The mean is
4+
* simply the average of the numbers. The standard deviation is a statistic that tells
5+
* you how tightly all the various data are clustered around the mean in a set of data.
6+
* For example, what is the average age of the students in a class? How close are the
7+
* ages? If all the students are the same age, the deviation is 0.
8+
* Write a program that prompts the user to enter 10 numbers and displays the mean
9+
* and standard deviations of these numbers using the following formula:
10+
*
11+
* Here is a sample run:
12+
* Enter 10 numbers: 1 2 3 4.5 5.6 6 7 8 9 10
13+
* The mean is 5.61
14+
* The standard deviation is 2.99794
15+
*
16+
* Created by Sven on 06/05/19.
17+
*/
18+
package Chapter05;
19+
20+
import java.util.Scanner;
21+
22+
public class Exercise0545_Statistics_compute_mean_and_standard_deviation {
23+
public static void main(String[] args) {
24+
Scanner input = new Scanner(System.in);
25+
System.out.print("Enter 10 numbers: ");
26+
27+
double sum = 0.0;
28+
double square_sum = 0.0;
29+
int count = 0;
30+
31+
for (int i = 0; i < 10; i++) {
32+
double next = input.nextDouble();
33+
sum += next;
34+
square_sum += Math.pow(next, 2);
35+
count++;
36+
}
37+
38+
System.out.println("The mean is " + sum / count);
39+
System.out.println("The standard deviation is " + Math.sqrt((square_sum - (Math.pow(sum, 2) / count)) / (count - 1)));
40+
}
41+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* (Reverse a string) Write a program that prompts the user to enter a string and
3+
* displays the string in reverse order.
4+
*
5+
* Enter a string: ABCD
6+
* The reversed string is DCBA
7+
*
8+
* Created by Sven on 06/05/19.
9+
*/
10+
package Chapter05;
11+
12+
import java.util.Scanner;
13+
14+
public class Exercise0546_Reverse_a_string {
15+
public static void main(String[] args) {
16+
Scanner input = new Scanner(System.in);
17+
System.out.print("Enter a string: ");
18+
19+
String str = input.next();
20+
String reverse_str = "";
21+
22+
for (int i = str.length() - 1; i >= 0; i--) {
23+
reverse_str += str.charAt(i);
24+
}
25+
System.out.print("The reversed string is " + reverse_str);
26+
27+
}
28+
}

0 commit comments

Comments
 (0)