Skip to content

Commit 1bb5c43

Browse files
author
Sarah Akinkunmi
committed
updates
1 parent 98fa772 commit 1bb5c43

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2041
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
4.32 (Checkerboard Pattern of Asterisks) Write an application that uses only the output statements
3+
System.out.print("* ");
4+
System.out.print(" ");
5+
System.out.println();
6+
to display the checkerboard pattern that follows. A System.out.println method call with no arguments causes the program to output a single newline character. [Hint: Repetition statements are
7+
required.]
8+
9+
* * * * * * * *
10+
* * * * * * * *
11+
* * * * * * * *
12+
* * * * * * * *
13+
* * * * * * * *
14+
* * * * * * * *
15+
* * * * * * * *
16+
* * * * * * * *
17+
*/
18+
19+
package chapter4;
20+
21+
import java.util.Scanner;
22+
23+
public class CheckerboardPatternOfAsterisks {
24+
public static void main(String[] args) {
25+
Scanner input = new Scanner(System.in);
26+
int size = input.nextInt();
27+
int lines = size;
28+
29+
for(int i = 0; i < lines; i++) {
30+
if(i % 2 == 0) {
31+
for(int shape = 0; shape < size; shape++) {
32+
System.out.print("* ");
33+
System.out.print(" ");
34+
}
35+
} else if (i % 2 != 0) {
36+
for(int shape = 0; shape < size; shape++) {
37+
System.out.print(" ");
38+
System.out.print("* ");
39+
}
40+
}
41+
System.out.println();
42+
}
43+
}
44+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
4.18 (Credit Limit Calculator) Develop a Java application that determines whether any of several
3+
department-store customers has exceeded the credit limit on a charge account. For each customer,
4+
the following facts are available:
5+
a) account number
6+
b) balance at the beginning of the month
7+
c) total of all items charged by the customer this month
8+
d) total of all credits applied to the customer’s account this month
9+
e) allowed credit limit
10+
11+
The program should input all these facts as integers, calculate the new balance (= beginning balance
12+
+ charges – credits), display the new balance and determine whether the new balance exceeds the
13+
customer’s credit limit. For those customers whose credit limit is exceeded, the program should display the message "Credit limit exceeded".
14+
*/
15+
16+
package chapter4;
17+
18+
import java.util.Scanner;
19+
20+
public class CreditLimitCalculator {
21+
public static void main(String[] args) {
22+
Scanner input = new Scanner(System.in);
23+
24+
int accountNumber;
25+
int balance;
26+
int totalCharges;
27+
int totalCredits;
28+
int creditLimit;
29+
30+
System.out.println("Enter customer's account number: ");
31+
accountNumber = input.nextInt();
32+
33+
System.out.println("Enter customer's beginning balance: ");
34+
balance = input.nextInt();
35+
36+
System.out.println("Enter total of all items charged by customer this month: ");
37+
totalCharges = input.nextInt();
38+
39+
System.out.println("Enter total of all credits applied to the customer’s account this month: ");
40+
totalCredits = input.nextInt();
41+
42+
System.out.println("Enter credit limit applicable to customer: ");
43+
creditLimit = input.nextInt();
44+
45+
while (creditLimit > balance) {
46+
balance += totalCredits - totalCharges;
47+
System.out.println("The customer's new balance is " + balance);
48+
}
49+
50+
if(balance > creditLimit)
51+
System.out.println("Credit Limit Exceeded!");
52+
}
53+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
4.31 (Printing the Decimal Equivalent of a Binary Number) Write an application that inputs an
3+
integer containing only 0s and 1s (i.e., a binary integer) and prints its decimal equivalent. [Hint: Use
4+
the remainder and division operators to pick off the binary number’s digits one at a time, from right
5+
to left. In the decimal number system, the rightmost digit has a positional value of 1 and the next
6+
digit to the left a positional value of 10, then 100, then 1000, and so on. The decimal number 234
7+
can be interpreted as 4 * 1 + 3 * 10 + 2 * 100. In the binary number system, the rightmost digit has
8+
a positional value of 1, the next digit to the left a positional value of 2, then 4, then 8, and so on.
9+
The decimal equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8 or, 13.]
10+
*/
11+
12+
package chapter4;
13+
14+
import java.util.Scanner;
15+
16+
public class DecimalEquivalentOfBinary {
17+
public static void main(String[] args) {
18+
Scanner input = new Scanner(System.in);
19+
int binaryNumber = input.nextInt();
20+
21+
int decimalNumber = 0;
22+
23+
if(binaryNumber >= 10 && binaryNumber <= 99) {
24+
int firstDigit = binaryNumber % 10;
25+
int secondDigit = binaryNumber / 10;
26+
27+
if(firstDigit == 1 || firstDigit == 0 && (secondDigit == 1 || secondDigit == 0)) {
28+
decimalNumber = firstDigit + secondDigit * 2;
29+
}
30+
System.out.println("The decimal equivalent of " + binaryNumber + "is " + decimalNumber);
31+
} else if(binaryNumber >= 100 && binaryNumber <= 999) {
32+
int firstDigit = binaryNumber % 10;
33+
int secondDigit = binaryNumber / 10 % 10;
34+
int thirdDigit = binaryNumber / 100;
35+
36+
if(firstDigit == 1 || firstDigit == 0 && (secondDigit == 1 || secondDigit == 0) && (thirdDigit == 1 || thirdDigit == 0)) {
37+
decimalNumber = firstDigit + secondDigit * 2 + thirdDigit * 4;
38+
}
39+
System.out.println("The decimal equivalent of " + binaryNumber + " is " + decimalNumber);
40+
} else if(binaryNumber >= 1000 && binaryNumber <= 9999) {
41+
int firstDigit = binaryNumber % 10;
42+
int secondDigit = binaryNumber / 10 % 10;
43+
int thirdDigit = binaryNumber / 100 % 10;
44+
int fourthDigit = binaryNumber / 1000;
45+
46+
if(firstDigit == 1 || firstDigit == 0 && (secondDigit == 1 || secondDigit == 0) && (thirdDigit == 1 || thirdDigit == 0) && (fourthDigit == 1 || fourthDigit == 0)) {
47+
decimalNumber = firstDigit + secondDigit * 2 + thirdDigit * 4 + fourthDigit * 8;
48+
}
49+
System.out.println("The decimal equivalent of " + binaryNumber + " is " + decimalNumber);
50+
} else if(binaryNumber >= 10000 && binaryNumber <= 99999) {
51+
int firstDigit = binaryNumber % 10;
52+
int secondDigit = binaryNumber / 10 % 10;
53+
int thirdDigit = binaryNumber / 100 % 10;
54+
int fourthDigit = binaryNumber / 1000 % 10;
55+
int fifthDigit = binaryNumber / 10000;
56+
57+
if (firstDigit == 1 || firstDigit == 0 && (secondDigit == 1 || secondDigit == 0) && (thirdDigit == 1 || thirdDigit == 0) && (fourthDigit == 1 || fourthDigit == 0) && (fifthDigit == 1 || fifthDigit == 0)) {
58+
decimalNumber = firstDigit + secondDigit * 2 + thirdDigit * 4 + fourthDigit * 8 + fifthDigit * 16;
59+
}
60+
System.out.println("The decimal equivalent of " + binaryNumber + " is " + decimalNumber);
61+
} else {
62+
System.out.println("Number is not a binary number");
63+
}
64+
65+
}
66+
}

src/chapter4/Decrypting.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package chapter4;
2+
3+
public class Decrypting {
4+
public void decoder(int userInput){
5+
int decryptedFirstDigit = 0;
6+
int decryptedSecondDigit = 0;
7+
int decryptedThirdDigit = 0;
8+
int decryptedFourthDigit = 0;
9+
10+
int encryptedFirstDigit = userInput % 100 / 10;
11+
int encryptedSecondDigit = userInput % 10;
12+
int encryptedThirdDigit = userInput / 1000;
13+
int encryptedFourthDigit = userInput % 1000 / 100;
14+
15+
for(int possibleAnswer = 0; possibleAnswer <= 9; possibleAnswer++){
16+
if ((possibleAnswer + 7) % 10 == encryptedFirstDigit){ decryptedFirstDigit = possibleAnswer; }
17+
if ((possibleAnswer + 7) % 10 == encryptedSecondDigit){ decryptedSecondDigit = possibleAnswer; }
18+
if ((possibleAnswer + 7) % 10 == encryptedThirdDigit){ decryptedThirdDigit = possibleAnswer; }
19+
if ((possibleAnswer + 7) % 10 == encryptedFourthDigit){ decryptedFourthDigit = possibleAnswer; }
20+
}
21+
int decryptedInteger = (decryptedFirstDigit * 1000 + decryptedSecondDigit * 100 + decryptedThirdDigit * 10 + decryptedFourthDigit);
22+
System.out.println(decryptedInteger);
23+
}
24+
}

src/chapter4/DecryptingTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package chapter4;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class DecryptingTest {
7+
private Decrypting decoder;
8+
9+
@BeforeEach
10+
void startTestsWith() {
11+
decoder = new Decrypting();
12+
}
13+
14+
@Test
15+
void testThatUserInputsFourDigitNumber(){
16+
decoder.decoder(1234);
17+
}
18+
}

src/chapter4/Decryptor.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
4.38 (Enforcing Privacy with Cryptography) The explosive growth of Internet communications
3+
and data storage on Internet-connected computers has greatly increased privacy concerns. The field
4+
of cryptography is concerned with coding data to make it difficult (and hopefully—with the most
5+
advanced schemes—impossible) for unauthorized users to read. In this exercise you’ll investigate a
6+
simple scheme for encrypting and decrypting data. A company that wants to send data over the Internet
7+
has asked you to write a program that will encrypt it so that it may be transmitted more securely.
8+
All the data is transmitted as four-digit integers. Your application should read a four-digit
9+
integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 7
10+
to the digit and getting the remainder after dividing the new value by 10. Then swap the first digit
11+
with the third, and swap the second digit with the fourth. Then print the encrypted integer. Write
12+
a separate application that inputs an encrypted four-digit integer and decrypts it (by reversing the
13+
encryption scheme) to form the original number. [Optional reading project: Research “public key
14+
cryptography” in general and the PGP (Pretty Good Privacy) specific public key scheme. You may
15+
also want to investigate the RSA scheme, which is widely used in industrial-strength applications.]
16+
*/
17+
18+
package chapter4;
19+
20+
import java.util.Scanner;
21+
22+
public class Decryptor {
23+
public static void main(String[] args) {
24+
Scanner input = new Scanner(System.in);
25+
System.out.println("Enter a four-digit number: ");
26+
int initialNumber = input.nextInt();
27+
28+
int decryptedFirstDigit = 0;
29+
int decryptedSecondDigit = 0;
30+
int decryptedThirdDigit = 0;
31+
int decryptedFourthDigit = 0;
32+
33+
int encryptedFirstDigit = initialNumber % 100 / 10;
34+
int encryptedSecondDigit = initialNumber % 10;
35+
int encryptedThirdDigit = initialNumber / 1000;
36+
int encryptedFourthDigit = initialNumber % 1000 / 100;
37+
38+
for(int possibleAnswer = 0; possibleAnswer <= 9; possibleAnswer++){
39+
if ((possibleAnswer + 7) % 10 == encryptedFirstDigit){ decryptedFirstDigit = possibleAnswer; }
40+
if ((possibleAnswer + 7) % 10 == encryptedSecondDigit){ decryptedSecondDigit = possibleAnswer; }
41+
if ((possibleAnswer + 7) % 10 == encryptedThirdDigit){ decryptedThirdDigit = possibleAnswer; }
42+
if ((possibleAnswer + 7) % 10 == encryptedFourthDigit){ decryptedFourthDigit = possibleAnswer; }
43+
}
44+
int decryptedInteger = (decryptedFirstDigit * 1000 + decryptedSecondDigit * 100 + decryptedThirdDigit * 10 + decryptedFourthDigit);
45+
System.out.println("The decrypted number is " + decryptedInteger);
46+
}
47+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package chapter4;
2+
3+
import java.util.Scanner;
4+
5+
public class DoWhileStudentAverageGrade {
6+
7+
public static void main(String[] args){
8+
Scanner input = new Scanner(System.in);
9+
int studentGrade;
10+
int totalGrade = 0;
11+
int numberOfGrades = 0;
12+
13+
do{
14+
System.out.print("Enter student grade: ");
15+
studentGrade = input.nextInt();
16+
totalGrade += studentGrade;
17+
18+
numberOfGrades++;
19+
}
20+
while(numberOfGrades < 10);
21+
22+
System.out.print(totalGrade);
23+
24+
double averageGrade = totalGrade / (numberOfGrades * 1.0);
25+
System.out.println(averageGrade);
26+
27+
}
28+
29+
30+
}

src/chapter4/EConstantFactorial.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
4.37 (Factorial) The factorial of a non-negative integer n is written as n!
3+
(pronounced “n factorial”) and is defined as follows
4+
5+
n! = n · (n – 1) · (n – 2) · … · 1 (for values of n greater than or equal to 1)
6+
and
7+
n! = 1 (for n = 0)
8+
For example, 5! = 5 · 4 · 3 · 2 · 1, which is 120.
9+
10+
b) Write an application that estimates the value of the mathematical constant e by using
11+
the following formula. Allow the user to enter the number of terms to calculate.
12+
13+
e = 1 + 1/1! + 1/2! + 1/3!...
14+
15+
c) Write an application that computes the value of e^(x)nby using the following formula. Allow the user to enter the number of terms to calculate.
16+
e^(x) = 1 + x/1! + x^2/2! + x^3/3!...
17+
*/
18+
19+
package chapter4;
20+
21+
public class EConstantFactorial {
22+
public static int factorial(int n) {
23+
int i = 1;
24+
int solution = 1;
25+
while (i <= n) {
26+
solution = solution * i;
27+
i++;
28+
}
29+
return solution;
30+
}
31+
32+
public static double getEConstant(int userConstraint) {
33+
double e = 1;
34+
for(int i = 1; i <= userConstraint; i++)
35+
e += 1.0 / factorial(i);
36+
return e;
37+
}
38+
39+
public static double getEConstantToAPower(int userConstraint, int power) {
40+
double e = 1;
41+
for(int i = 1; i <= userConstraint; i++)
42+
e += Math.pow(power, i) / factorial(i);
43+
return e;
44+
}
45+
}
46+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package chapter4;
2+
3+
public class EConstantFactorialTest {
4+
public static void main(String[] args) {
5+
System.out.printf("%s%f%n", "The value of e is ", EConstantFactorial.getEConstant(3));
6+
System.out.printf("%s%f", "The value of e^(2) is ", EConstantFactorial.getEConstantToAPower(3, 2));
7+
}
8+
}

src/chapter4/Encryptor.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
4.38 (Enforcing Privacy with Cryptography) The explosive growth of Internet communications
3+
and data storage on Internet-connected computers has greatly increased privacy concerns. The field
4+
of cryptography is concerned with coding data to make it difficult (and hopefully—with the most
5+
advanced schemes—impossible) for unauthorized users to read. In this exercise you’ll investigate a
6+
simple scheme for encrypting and decrypting data. A company that wants to send data over the Internet
7+
has asked you to write a program that will encrypt it so that it may be transmitted more securely.
8+
All the data is transmitted as four-digit integers. Your application should read a four-digit
9+
integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 7
10+
to the digit and getting the remainder after dividing the new value by 10. Then swap the first digit
11+
with the third, and swap the second digit with the fourth. Then print the encrypted integer. Write
12+
a separate application that inputs an encrypted four-digit integer and decrypts it (by reversing the
13+
encryption scheme) to form the original number. [Optional reading project: Research “public key
14+
cryptography” in general and the PGP (Pretty Good Privacy) specific public key scheme. You may
15+
also want to investigate the RSA scheme, which is widely used in industrial-strength applications.]
16+
17+
*/
18+
19+
package chapter4;
20+
21+
import java.util.Scanner;
22+
23+
public class Encryptor {
24+
public static void main(String[] args) {
25+
Scanner input = new Scanner(System.in);
26+
System.out.println("Enter a four-digit number: ");
27+
28+
int initialNumber = input.nextInt();
29+
30+
if(initialNumber >= 1000 && initialNumber < 10000){
31+
int firstDigit = initialNumber / 1000;
32+
firstDigit += 7;
33+
firstDigit %= 10;
34+
35+
int secondDigit = initialNumber % 1000 / 100;
36+
secondDigit += 7;
37+
secondDigit %= 10;
38+
39+
int thirdDigit = initialNumber % 100 / 10;
40+
thirdDigit += 7;
41+
thirdDigit %= 10;
42+
43+
int fourthDigit = initialNumber % 10;
44+
fourthDigit += 7;
45+
fourthDigit %= 10;
46+
47+
int encryptedInteger = thirdDigit * 1000 + fourthDigit * 100 + firstDigit * 10 + secondDigit;
48+
System.out.println("The encrypted number is " + encryptedInteger);
49+
}
50+
51+
52+
}
53+
}

0 commit comments

Comments
 (0)