Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit 2f7cc71

Browse files
committed
Java after Part 1A and 9/10 chapters of Deitel
0 parents  commit 2f7cc71

356 files changed

Lines changed: 17480 additions & 0 deletions

File tree

Some content is hidden

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

.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Thank you GitHub
2+
*.class
3+
4+
# Mobile Tools for Java (J2ME)
5+
.mtj.tmp/
6+
7+
# Package Files
8+
*.jar
9+
*.war
10+
*.ear
11+
12+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
13+
hs_err_pid*
14+
15+
# java1A
16+
java1A/.metadata/**
17+
18+
# Deitel and Deitel 10th
19+
deitel-10th/examples/**

deitel-10th/DrawPanel.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// GUI Exercise for Chapter 10 using inheritance.
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics;
5+
import java.util.Arrays;
6+
import java.util.Random;
7+
import javax.swing.JPanel;
8+
9+
public class DrawPanel extends JPanel {
10+
private final MyShape[] shapes;
11+
private final int[] total_shapes = new int[3];
12+
13+
public int getLinesNum() { return total_shapes[0]; }
14+
public int getRectsNum() { return total_shapes[1]; }
15+
public int getOvalsNum() { return total_shapes[2]; }
16+
17+
public DrawPanel(int shapes_num) {
18+
19+
Random rand = new Random();
20+
setBackground(Color.WHITE);
21+
shapes = new MyShape[shapes_num];
22+
Arrays.fill(total_shapes, 0);
23+
24+
for (int count = 0; count < shapes.length; count++) {
25+
26+
int x1 = rand.nextInt(300);
27+
int y1 = rand.nextInt(300);
28+
int x2 = rand.nextInt(300);
29+
int y2 = rand.nextInt(300);
30+
31+
Color color = new Color(rand.nextInt(256),
32+
rand.nextInt(256), rand.nextInt(256));
33+
34+
total_shapes[x1 % 3]++;
35+
36+
if (x1 % 3 == 0)
37+
shapes[count] = new MyLine(x1, y1, x2, y2, color);
38+
else if (x1 % 3 == 1)
39+
shapes[count] = new MyRectangle(x1,y1,x2,y2,color, x2 % 2 == 0);
40+
else
41+
shapes[count] = new MyOval(x1,y1,x2,y2,color, x2 % 2 == 0);
42+
43+
}
44+
45+
}
46+
47+
48+
public void paintComponent(Graphics g) {
49+
super.paintComponent(g);
50+
51+
for (MyShape shape : shapes)
52+
shape.draw(g);
53+
}
54+
}

deitel-10th/MyBoundedShape.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// GUI Exercise for Chapter 10 using inheritance.
2+
3+
import java.awt.Color;
4+
5+
public abstract class MyBoundedShape extends MyShape {
6+
private boolean filled;
7+
8+
public boolean getFilled() { return filled; }
9+
10+
public void setFilled(boolean f) { filled = f; }
11+
12+
// implicit call to super();
13+
public MyBoundedShape() { this.filled = false; }
14+
15+
public MyBoundedShape(int x1, int y1, int x2, int y2,
16+
Color color, boolean filled) {
17+
super(x1, y1, x2, y2, color);
18+
this.filled = filled;
19+
}
20+
21+
public int getUpperLeftX() {
22+
return Math.min(getX1(),getX2());
23+
}
24+
25+
public int getUpperLeftY() {
26+
return Math.min(getY1(),getY2());
27+
}
28+
29+
public int getWidth() {
30+
return Math.abs(getX2()-getX1()); }
31+
32+
public int getHeight() {
33+
return Math.abs(getY2()-getY1());
34+
}
35+
}

deitel-10th/MyLine.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// GUI Exercise for Chapter 8.
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics;
5+
6+
public class MyLine extends MyShape {
7+
public MyLine() { /* implicit call to super(); */ }
8+
9+
public MyLine(int x1, int y1, int x2, int y2, Color color) {
10+
super(x1,y1,x2,y2,color);
11+
}
12+
13+
public void draw(Graphics g) {
14+
g.setColor(getColor());
15+
g.drawLine(getX1(), getY1(), getX2(), getY2());
16+
}
17+
}

deitel-10th/MyOval.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// GUI Exercise for Chapter 10 using inheritance.
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics;
5+
6+
public class MyOval extends MyBoundedShape {
7+
public MyOval() { /* implicit super(); */ }
8+
9+
public MyOval(int x1, int y1, int x2, int y2,
10+
Color color, boolean filled) {
11+
super(x1, y1, x2, y2, color, filled);
12+
}
13+
14+
public void draw(Graphics g) {
15+
g.setColor(getColor());
16+
if (getFilled())
17+
g.fillOval(getUpperLeftX(), getUpperLeftY(),
18+
getWidth(), getHeight());
19+
else
20+
g.drawOval(getUpperLeftX(), getUpperLeftY(),
21+
getWidth(), getHeight());
22+
}
23+
}

deitel-10th/MyRectangle.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// GUI Exercise for Chapter 8.
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics;
5+
6+
public class MyRectangle extends MyBoundedShape {
7+
public MyRectangle() { /* implicit super(); */ }
8+
9+
public MyRectangle(int x1, int y1, int x2, int y2,
10+
Color color, boolean filled) {
11+
super(x1, y1, x2, y2, color, filled);
12+
}
13+
14+
public void draw(Graphics g) {
15+
g.setColor(getColor());
16+
if (getFilled())
17+
g.fillRect(getUpperLeftX(), getUpperLeftY(),
18+
getWidth(), getHeight());
19+
else
20+
g.drawRect(getUpperLeftX(), getUpperLeftY(),
21+
getWidth(), getHeight());
22+
}
23+
}

deitel-10th/MyShape.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// GUI Ex 10.1: Introducing inheritance.
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics;
5+
6+
public abstract class MyShape {
7+
private int x1;
8+
private int y1;
9+
private int x2;
10+
private int y2;
11+
private Color color;
12+
13+
public int getX1() { return x1; }
14+
public int getX2() { return x2; }
15+
public int getY1() { return y1; }
16+
public int getY2() { return y2; }
17+
public Color getColor() { return color; }
18+
19+
private static int check(int a) { return a > 0 ? a : 0; }
20+
21+
public void setX1 (int x) { x1 = check(x); }
22+
public void setY1 (int y) { y1 = check(y); }
23+
public void setX2 (int x) { x2 = check(x); }
24+
public void setY2 (int y) { y2 = check(y); }
25+
public void setColor(Color c) { color = c; }
26+
27+
public MyShape() { this(0,0,0,0,Color.BLACK); }
28+
29+
public MyShape(int x1, int y1, int x2, int y2, Color color) {
30+
this.x1 = check(x1);
31+
this.y1 = check(y1);
32+
this.x2 = check(x2);
33+
this.y2 = check(y2);
34+
this.color = color;
35+
}
36+
37+
public abstract void draw(Graphics g);
38+
39+
}

deitel-10th/Test.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Ex 8.??: TODO
2+
3+
import java.util.Scanner;
4+
5+
public class Test {
6+
private static final Scanner input = new Scanner(System.in);
7+
public static void main(String[] args) {
8+
9+
}
10+
11+
private static int getPosInt(int lower, int upper, String msg) {
12+
int x = input.nextInt();
13+
while (x < lower || upper < x) {
14+
System.out.println(msg);
15+
x = input.nextInt();
16+
}
17+
return x;
18+
}
19+
}

deitel-10th/TestDraw.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// GUI Ex 10: TestDraw.java for inherited classes.
2+
3+
import java.awt.BorderLayout;
4+
import javax.swing.JFrame;
5+
import javax.swing.JLabel;
6+
import javax.swing.JOptionPane;
7+
8+
public class TestDraw
9+
{
10+
public static void main(String[] args)
11+
{
12+
int i = Integer.parseInt(
13+
JOptionPane.showInputDialog(
14+
"How many shapes do you want?"));
15+
while (i < 1) {
16+
i = Integer.parseInt(
17+
JOptionPane.showInputDialog(
18+
"Number must be 1 or greater."));
19+
}
20+
21+
DrawPanel panel = new DrawPanel(i);
22+
JFrame app = new JFrame();
23+
JLabel count = new JLabel(
24+
String.format("Lines: %d, Rectangles: %d, Ovals: %d",
25+
panel.getLinesNum(), panel.getRectsNum(), panel.getOvalsNum()));
26+
27+
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
28+
app.add(panel);
29+
app.add(count, BorderLayout.SOUTH);
30+
app.setSize(300, 300);
31+
app.setVisible(true);
32+
}
33+
} // end class TestDraw
34+
35+
36+
/**************************************************************************
37+
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
38+
* Pearson Education, Inc. All Rights Reserved. *
39+
* *
40+
* DISCLAIMER: The authors and publisher of this book have used their *
41+
* best efforts in preparing the book. These efforts include the *
42+
* development, research, and testing of the theories and programs *
43+
* to determine their effectiveness. The authors and publisher make *
44+
* no warranty of any kind, expressed or implied, with regard to these *
45+
* programs or to the documentation contained in these books. The authors *
46+
* and publisher shall not be liable in any event for incidental or *
47+
* consequential damages in connection with, or arising out of, the *
48+
* furnishing, performance, or use of these programs. *
49+
*************************************************************************/

deitel-10th/ch01-02/Arith.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Ex 2.15: Enter two integers and return sum, difference, product and quotient.
2+
3+
import java.util.Scanner;
4+
5+
public class Arith {
6+
public static void main(String[] args) {
7+
8+
Scanner input = new Scanner(System.in);
9+
10+
System.out.print("Enter first integer: ");
11+
int a = input.nextInt();
12+
13+
System.out.print("Enter second integer: ");
14+
int b = input.nextInt();
15+
16+
System.out.printf("Sum: %d%nDifference: %d%nProduct: %d%nQuotient: %d%n", a+b, a-b, a*b, a/b);
17+
}
18+
}

0 commit comments

Comments
 (0)