Skip to content

Commit 9811938

Browse files
committed
Introduction to Computers and Java
1 parent a63d0f4 commit 9811938

File tree

30 files changed

+769
-0
lines changed

30 files changed

+769
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package chapter_01;
2+
3+
/**
4+
* 6. Write a statement or statements that can be used in a Java program to
5+
* display the following on the screen:
6+
* Welcome
7+
* To
8+
* Java
9+
*
10+
* @author Sharaf Qeshta
11+
* */
12+
13+
public class Exercise_01_06
14+
{
15+
public static void main(String[] args)
16+
{
17+
System.out.println("Welcome");
18+
System.out.println("To");
19+
System.out.println("Java");
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package chapter_01;
2+
3+
/**
4+
* 7. What would the following statements, when used in a Java program,
5+
* display on the screen?
6+
* int a = 10;
7+
* int a2 = 100;
8+
* System.out.println(a2);
9+
* System.out.println (" is square of ");
10+
* System.out.println(a);
11+
*
12+
* @author Sharaf Qeshta
13+
* */
14+
public class Exercise_01_07
15+
{
16+
public static void main(String[] args)
17+
{
18+
// 100
19+
// is square of
20+
// 10
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package chapter_01;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* 8. Write statements that can be used in a Java program to read two numbers,
7+
* as entered at the keyboard, and display their difference on the screen.
8+
*
9+
* @author Sharaf Qeshta
10+
* */
11+
public class Exercise_01_08
12+
{
13+
public static void main(String[] args)
14+
{
15+
Scanner scanner = new Scanner(System.in);
16+
17+
int x = scanner.nextInt(), y = scanner.nextInt();
18+
19+
System.out.println(x + " - " + y + " = " + (x - y));
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package chapter_01;
2+
3+
/**
4+
* 9. Given a person’s year of birth, the Election Wizard can compute the year
5+
* in which they will be eligible to vote (assuming the eligibility age to be 18
6+
* years). Write statements that can be used in a Java program to perform this
7+
* computation for the Election Wizard.
8+
*
9+
* @author Sharaf Qeshta
10+
* */
11+
public class Exercise_01_09
12+
{
13+
public static void main(String[] args)
14+
{
15+
int yearOfBirth = 2001;
16+
17+
System.out.println(2022 - yearOfBirth);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package chapter_01;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* 10. Write statements that can be used in a Java program to read two integers and
7+
* display the number of integers that lie between them, including the integers
8+
* themselves. For example, four integers are between 3 and 6: 3, 4, 5, and 6.
9+
*
10+
* @author Sharaf Qeshta
11+
* */
12+
public class Exercise_01_10
13+
{
14+
public static void main(String[] args)
15+
{
16+
Scanner scanner = new Scanner(System.in);
17+
18+
System.out.print("Enter two integers: ");
19+
int x = scanner.nextInt(), y = scanner.nextInt();
20+
21+
int integersCount = y - x + 1;
22+
23+
System.out.println("Integers count lie between " + x + " and " + y + " is " + integersCount);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package chapter_01;
2+
3+
/**
4+
* 11. A memory address starts with 2313. Each subsequent value uses three
5+
* bytes as location. For instance, the second value takes memory
6+
* addresses 2316, 2317, and 2318. What memory addresses will be taken
7+
* by:
8+
* a. 5th value? b. 10th value? c. 13th value?
9+
*
10+
* @author Sharaf Qeshta
11+
* */
12+
13+
public class Exercise_01_11
14+
{
15+
// we can extract a pattern
16+
// nth value last memory location is (n * 3) + 2312
17+
// 1st value last memory location is 3 + 2312 = 2315
18+
// 2nd value last memory location is 6 + 2312 = 2318
19+
// and so on
20+
21+
// a. 5th
22+
// 15 + 2312 = 2327
23+
// 2325, 2326, 2327
24+
25+
// b. 10th
26+
// 30 + 2312 = 2342
27+
// 2340, 2341, 2342
28+
29+
// c. 13th
30+
// 39 + 2312 = 2351
31+
// 2349, 2350, 2351
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package chapter_01;
2+
3+
/**
4+
* 12. Find the documentation for the Java Class Library on the Oracle® Web site.
5+
* (At this writing, the link to this documentation is https://docs.oracle.com/
6+
* javase/8/docs/api/.) Then find the description for the class Scanner. How
7+
* many methods are described in the section entitled “Method Summary”?
8+
*
9+
* @author Sharaf Qeshta
10+
* */
11+
public class Exercise_01_12
12+
{
13+
// 55 methods exist in the Scanner class
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package chapter_01;
2+
3+
/**
4+
* 13. Self-Test Question 27 asked you to think of some attributes for a song
5+
* object. What attributes would you want for an object that represents a play
6+
* list containing many songs?
7+
*
8+
* @author Sharaf Qeshta
9+
* */
10+
public class Exercise_01_13
11+
{
12+
// an array of songs
13+
// current playing song
14+
// loop or not
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package chapter_01;
2+
3+
/**
4+
* 14. What behaviors might a song have? What behaviors might a play list have?
5+
* Contrast the difference in behavior between the two kinds of objects.
6+
*
7+
* @author Sharaf Qeshta
8+
* */
9+
10+
public class Exercise_01_14
11+
{
12+
// song
13+
// 1. play
14+
// 2. pause
15+
// 3. seek
16+
17+
// playlist
18+
// 1. play
19+
// 2. play specific song
20+
// 3. set the loop
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package chapter_01;
2+
3+
/**
4+
* 15. What attributes and behaviors would an object representing a mobile sim
5+
* card have?
6+
*
7+
* @author Sharaf Qeshta
8+
* */
9+
public class Exercise_01_15
10+
{
11+
// attributes
12+
// 1. card number
13+
// 2. list of contacts
14+
// 3. service provider
15+
16+
// behaviors
17+
// 1. make a call
18+
// 2. end a call
19+
// 3. store a contact
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package chapter_01;
2+
3+
/**
4+
* 16. Suppose that you have a number x that is greater than 1. Write an algorithm
5+
* that computes the largest integer k such that 2^k
6+
* is less than or equal to x.
7+
*
8+
* @author Sharaf Qeshta
9+
* */
10+
public class Exercise_01_16
11+
{
12+
// let k = 1
13+
// increment the k by one until 2^k > x
14+
// then the number is (k-1)
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package chapter_01;
2+
3+
/**
4+
* 17. Write an algorithm that finds the minimum value in a list of values
5+
*
6+
* @author Sharaf Qeshta
7+
* */
8+
public class Exercise_01_17
9+
{
10+
// let min = the first element in the list
11+
// for each x in the list if x < min then
12+
// min = x
13+
// when you finish then the minimum value will be
14+
// the value stored in min
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package chapter_01;
2+
3+
import javafx.application.Application;
4+
import javafx.scene.Scene;
5+
import javafx.scene.layout.Pane;
6+
import javafx.scene.paint.Color;
7+
import javafx.scene.shape.Circle;
8+
import javafx.stage.Stage;
9+
10+
/**
11+
* 18. Write statements that can be used in a JavaFX application to draw a dart
12+
* board showing concentric circular rings and a bull’s eye. (The bull’s eye
13+
* should be a filled circle.)
14+
*
15+
* @author Sharaf Qeshta
16+
* */
17+
public class Exercise_01_18 extends Application
18+
{
19+
public static void main(String[] args)
20+
{
21+
launch(args);
22+
}
23+
24+
@Override
25+
public void start(Stage primaryStage)
26+
{
27+
Pane pane = new Pane();
28+
29+
// bull`s eye
30+
Circle bullEye = new Circle(250, 250, 5);
31+
bullEye.setFill(Color.BLACK);
32+
33+
// 6 circles around the bull`s eye
34+
Circle circle1 = new Circle(250, 250, 15, Color.WHITE);
35+
circle1.setStroke(Color.BLACK);
36+
37+
Circle circle2 = new Circle(250, 250, 25, Color.WHITE);
38+
circle2.setStroke(Color.BLACK);
39+
40+
Circle circle3 = new Circle(250, 250, 35, Color.WHITE);
41+
circle3.setStroke(Color.BLACK);
42+
43+
Circle circle4 = new Circle(250, 250, 45, Color.WHITE);
44+
circle4.setStroke(Color.BLACK);
45+
46+
Circle circle5 = new Circle(250, 250, 55, Color.WHITE);
47+
circle5.setStroke(Color.BLACK);
48+
49+
Circle circle6 = new Circle(250, 250, 65, Color.WHITE);
50+
circle6.setStroke(Color.BLACK);
51+
52+
pane.getChildren().addAll(circle6, circle5, circle4,
53+
circle3, circle2, circle1, bullEye);
54+
55+
Scene scene = new Scene(pane, 500, 500);
56+
primaryStage.setTitle("Exercise_01_18");
57+
primaryStage.setScene(scene);
58+
primaryStage.show();
59+
}
60+
}
17 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package chapter_01;
2+
3+
import javafx.application.Application;
4+
import javafx.scene.Group;
5+
import javafx.scene.Scene;
6+
import javafx.scene.canvas.Canvas;
7+
import javafx.scene.canvas.GraphicsContext;
8+
import javafx.stage.Stage;
9+
10+
11+
/**
12+
* 19. Find the documentation for the class Graphics Context in the JavaFX Class
13+
* Library. (See Exercise 12.) Learn how to use the method strokeRect. Then
14+
* write statements that can be used in a JavaFX application to draw a square
15+
* containing a circle. The circle’s diameter and the square’s side should be
16+
* equal in size.
17+
*
18+
* @author Sharaf Qeshta
19+
* */
20+
public class Exercise_01_19 extends Application
21+
{
22+
23+
public static void main(String[] args)
24+
{
25+
launch(args);
26+
}
27+
28+
@Override
29+
public void start(Stage primaryStage)
30+
{
31+
// https://docs.oracle.com/javase/8/javafx/api/javafx/scene/canvas/GraphicsContext.html#strokeRect-double-double-double-double-
32+
Group root = new Group();
33+
Canvas canvas = new Canvas(500, 500);
34+
GraphicsContext gc = canvas.getGraphicsContext2D();
35+
36+
// rectangle
37+
gc.strokeRect(245, 245, 100, 100);
38+
39+
// circle
40+
gc.strokeOval(245, 245, 100, 100);
41+
root.getChildren().add(canvas);
42+
43+
Scene scene = new Scene(root, 500, 500);
44+
primaryStage.setTitle("Exercise_01_19");
45+
primaryStage.setScene(scene);
46+
primaryStage.show();
47+
}
48+
}
9.47 KB
Loading

0 commit comments

Comments
 (0)