Skip to content

Commit abc9bbf

Browse files
committed
Basic Computation
1 parent 9811938 commit abc9bbf

File tree

33 files changed

+1142
-0
lines changed

33 files changed

+1142
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package chapter_02;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* 1. Write a program that demonstrates the approximate nature of double
7+
* values by performing the following tasks:
8+
* • Use Scanner to read a double value x.
9+
* • Compute 5.0 * x and store the result in y.
10+
* • Display x, y, and the sum of x and y.
11+
* • Divide 5.0 from x and store the result in z. Display the value of z.
12+
* Try your program with values of x that range from 2e-11 to 2e11.
13+
*
14+
* @author Sharaf Qeshta
15+
* */
16+
public class Exercise_02_01
17+
{
18+
public static void main(String[] args)
19+
{
20+
/*
21+
* Enter a double value x: 2e11
22+
* X = 2.0E11
23+
* Y = 1.0E12
24+
* X + Y = 1.2E12
25+
* Z = 4.0E10
26+
* */
27+
Scanner scanner = new Scanner(System.in);
28+
29+
System.out.print("Enter a double value x: ");
30+
double x = scanner.nextDouble();
31+
32+
double y = x * 5.0;
33+
34+
System.out.println("X = " + x);
35+
System.out.println("Y = " + y);
36+
System.out.println("X + Y = " + (x + y));
37+
38+
double z = x/5.0;
39+
System.out.println("Z = " + z);
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package chapter_02;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* 2. Write a program that demonstrates type casting of double values by
7+
* performing the following tasks:
8+
* • Use Scanner to read a floating-point value x.
9+
* • Type cast x to an int value and store the result in y.
10+
* • Display x and y clearly labeled.
11+
* • Type cast x to a byte value and store the result in z.
12+
* • Display x and z clearly labeled.
13+
* Try your program with positive and negative values of x that range in
14+
* magnitude from 2e-11 to 2e11. What can you conclude?
15+
*
16+
* @author Sharaf Qeshta
17+
* */
18+
public class Exercise_02_02
19+
{
20+
public static void main(String[] args)
21+
{
22+
/*
23+
* Enter a double value x: 56.256
24+
* X = 56.256
25+
* Y = 56
26+
* X = 56.256
27+
* Z = 56
28+
* */
29+
Scanner scanner = new Scanner(System.in);
30+
31+
System.out.print("Enter a double value x: ");
32+
double x = scanner.nextDouble();
33+
34+
int y = (int) x;
35+
System.out.println("X = " + x);
36+
System.out.println("Y = " + y);
37+
38+
byte z = (byte) x;
39+
System.out.println("X = " + x);
40+
System.out.println("Z = " + z);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package chapter_02;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* 3. Write a program that demonstrates the operator ++ by performing the
7+
* following tasks:
8+
* • Use Scanner to read an integer value x.
9+
* • Compute x++ and store the result in y.
10+
* • Display x and y clearly labeled.
11+
* • Compute ++x and store the result in z.
12+
* • Display x and z clearly labeled.
13+
* Try your program with positive and negative values of x. What can you
14+
* conclude about pre- and post-increment?
15+
*
16+
* @author Sharaf Qeshta
17+
* */
18+
public class Exercise_02_03
19+
{
20+
public static void main(String[] args)
21+
{
22+
/*
23+
* Enter an integer value x: 12
24+
* X = 13
25+
* Y = 12
26+
* X = 14
27+
* Z = 14
28+
* */
29+
Scanner scanner = new Scanner(System.in);
30+
31+
System.out.print("Enter an integer value x: ");
32+
int x = scanner.nextInt();
33+
34+
int y = x++;
35+
System.out.println("X = " + x);
36+
System.out.println("Y = " + y);
37+
38+
int z = ++x;
39+
System.out.println("X = " + x);
40+
System.out.println("Z = " + z);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package chapter_02;
2+
3+
4+
/**
5+
* 4. If u = 2, v = 3, w = 5, × = 7, and y = 11, what is the value of each of the
6+
* following expressions, assuming int variables?
7+
* • u + v * w + ×
8+
* • u + y % v * w + ×
9+
* • u++ / v + u++ * w
10+
*
11+
* @author Sharaf Qeshta
12+
* */
13+
public class Exercise_02_04
14+
{
15+
// u + v * w + ×
16+
/*
17+
* first compute v * w = 15
18+
* then
19+
* 2 + 15 + 7 = 24
20+
* */
21+
22+
// u + y % v * w + ×
23+
/*
24+
* since % * have the same preceding we will start
25+
* from left to right first compute y % v then multiply the result by w
26+
* 11 % 3 = 2
27+
* 2 * 5 = 10
28+
* 2 + 10 + 7 = 19
29+
* */
30+
31+
// u++ / v + u++ * w
32+
/*
33+
* 2 / 3 + 3 * 5
34+
* 2/3 = 0
35+
* 3 * 5 = 15
36+
* 0 + 15 = 15
37+
* */
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package chapter_02;
2+
3+
4+
import java.util.Scanner;
5+
6+
/**
7+
* 5. What changes to the ChangeMaker program in Listing 2.3 are necessary if it
8+
* also accepts coins for one dollar and half a dollar?
9+
*
10+
* @author Sharaf Qeshta
11+
* */
12+
public class Exercise_02_05
13+
{
14+
public static void main(String[] args) {
15+
int amount, originalAmount,
16+
dollars, halves, quarters, dimes, nickels, pennies; // here
17+
18+
System.out.println("Enter a whole number from 1 to 99.");
19+
System.out.println("I will find a combination of coins");
20+
System.out.println("that equals that amount of change.");
21+
Scanner keyboard = new Scanner(System.in);
22+
amount = keyboard.nextInt();
23+
originalAmount = amount;
24+
// here
25+
dollars = amount / 100;
26+
amount = amount % 100;
27+
halves = amount / 50;
28+
amount = amount % 50;
29+
30+
quarters = amount / 25;
31+
amount = amount % 25;
32+
dimes = amount / 10;
33+
amount = amount % 10;
34+
nickels = amount / 5;
35+
amount = amount % 5;
36+
pennies = amount;
37+
System.out.println(originalAmount +
38+
" cents in coins can be given as:");
39+
40+
// here
41+
System.out.println(dollars + " dollars");
42+
System.out.println(halves + " halves");
43+
44+
System.out.println(quarters + " quarters");
45+
System.out.println(dimes + " dimes");
46+
System.out.println(nickels + " nickels and");
47+
System.out.println(pennies + " pennies");
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package chapter_02;
2+
3+
4+
/**
5+
* 6. If the int variable × contains 10, what will the following Java statements
6+
* display?
7+
* System.out.println("Test 1" + × * 3 * 2.0);
8+
* System.out.println("Test 2" + × * 3 + 2.0);
9+
* Given these results, explain why the following Java statement will not
10+
* compile:
11+
* System.out.println("Test 3" + × * 3 − 2.0);
12+
*
13+
* @author Sharaf Qeshta
14+
* */
15+
public class Exercise_02_06
16+
{
17+
// System.out.println("Test 1" + × * 3 * 2.0);
18+
/*
19+
* 10 * 3 * 2.0 and concatenate it with Test 1
20+
* so the output will be
21+
* Test 160.0
22+
* since 10 * 3 * 2.0 = 60.0
23+
* */
24+
25+
// System.out.println("Test 2" + × * 3 + 2.0);
26+
/*
27+
* same as the previous one
28+
* Test 260.0
29+
* */
30+
31+
// System.out.println("Test 3" + × * 3 − 2.0);
32+
/*
33+
* as we know operations have precedence so
34+
* x * 3 will be evaluated fine
35+
* we still have "Test 3" + 30 - 2.0
36+
* because of the precedence we start from left to right
37+
* we will evaluate "Test 3" + 30 then
38+
* we will end up with a new string "Test 330"
39+
* as you know we cannot apply subtraction to strings
40+
* in java
41+
* */
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package chapter_02;
2+
3+
4+
/**
5+
* 7. Write some Java statements that use the String methods lastIndexOf and
6+
* substring to find the last word in a string. We define word to be a string of
7+
* characters that does not include whitespace. For example, the last word of the
8+
* string
9+
* "Hello, my good friend!"
10+
* is the string "friend!"
11+
*
12+
* @author Sharaf Qeshta
13+
* */
14+
public class Exercise_02_07
15+
{
16+
public static void main(String[] args)
17+
{
18+
String phrase = "Hello, my good friend!";
19+
String lastWord = phrase.substring(phrase.lastIndexOf(" ")+1);
20+
21+
/* The last word in the phrase "Hello, my good friend!" is "friend!" */
22+
System.out.println("The last word in the phrase \"" + phrase + "\" is \"" + lastWord + "\"" );
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package chapter_02;
2+
3+
4+
/**
5+
* 8. Repeat the previous exercise, but find the second last word in the string
6+
*
7+
* @author Sharaf Qeshta
8+
* */
9+
public class Exercise_02_08
10+
{
11+
public static void main(String[] args)
12+
{
13+
String phrase = "Hello, my good friend!";
14+
String lastWord = phrase.substring(phrase.lastIndexOf(" "));
15+
String newPhrase = phrase.replace(lastWord, "");
16+
lastWord = newPhrase.substring(newPhrase.lastIndexOf(" ")+1);
17+
18+
/* The second last word in the phrase "Hello, my good friend!" is "good" */
19+
System.out.println("The second last word in the phrase \"" + phrase + "\" is \"" + lastWord + "\"" );
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package chapter_02;
2+
3+
4+
/**
5+
* 9. What does the following Java statement display?
6+
* System.out.println("\"\\Release\"\\\tYour\n\\\'Stress\'\\\"\robot\"");
7+
* What is the significance of \R and \r here?
8+
*
9+
* @author Sharaf Qeshta
10+
* */
11+
public class Exercise_02_09
12+
{
13+
// "\Release"\ Your
14+
// obot"
15+
16+
// \r means back to the beginning of the current line
17+
// \R does not mean anything
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package chapter_02;
2+
3+
4+
/**
5+
* 10. Write a single Java statement that will display the words Alice, In, and
6+
* WonderLand, adding tab between the words and the third word within
7+
* double quotes.
8+
*
9+
* @author Sharaf Qeshta
10+
* */
11+
public class Exercise_02_10
12+
{
13+
public static void main(String[] args)
14+
{
15+
System.out.println("Alice,\tIn,\tand\t\"Wonderland\"");
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package chapter_02;
2+
3+
/**
4+
* 11. What does the Java code
5+
* Scanner keyboard = new Scanner(System.in);
6+
* System.out.println("Enter a value");
7+
* double val = keyboard.nextDouble();
8+
* System.out.println("Value entered is " + val);
9+
* display when the keyboard input is 4pi?
10+
*
11+
* @author Sharaf Qeshta
12+
* */
13+
public class Exercise_02_11
14+
{
15+
// Error the programme will deal with 4pi as string
16+
// so you enter a string while the programme expect a double value
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package chapter_02;
2+
3+
4+
/**
5+
* 12. What does the Java code
6+
* Scanner keyboard = new Scanner(System.in);
7+
* keyboard.useDelimiter("m");
8+
* System.out.println("Enter a string.");
9+
* String string1 = keyboard.next();
10+
* String string2 = keyboard.next();
11+
* System.out.println("String1 is " + string1);
12+
* System.out.println("String2 is " + string2);
13+
* display when the keyboard input is
14+
* Dream
15+
* Think
16+
* Imagine
17+
*
18+
*
19+
* @author Sharaf Qeshta
20+
* */
21+
public class Exercise_02_12
22+
{
23+
// String1 is Drea
24+
// String2 is Think I
25+
}

0 commit comments

Comments
 (0)