8000 Section 1,2 Complete · MhmDSmdi/Basic-Java-Course@72b9475 · GitHub
[go: up one dir, main page]

Skip to content

Commit 72b9475

Browse files
committed
Section 1,2 Complete
1 parent 77b09ad commit 72b9475

20 files changed

+437
-163
lines changed

.idea/workspace.xml

Lines changed: 179 additions & 93 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ Introduction to java programming for getting start android developing. This cour
2020
* Java Abstract Class
2121
* Java Polymorphism
2222
* Java Enums
23-
* Project1: Calculator!
23+
* Project1:
-606 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

src/Calculator.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/section1/Calculator.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package section1;
2+
3+
public class Calculator {
4+
5+
public double add(double a, double b) {
6+
return a + b;
7+
}
8+
9+
public double minis(double a, double b) {
10+
return a - b;
11+
}
12+
13+
public double multiple(double a, double b) {
14+
return a * b;
15+
}
16+
17+
public double division(double a, double b) {
18+
return a / b;
19+
}
20+
21+
public double sin(float theta) {
22+
theta = (float) (theta * 180 / Math.PI);
23+
return Math.sin(theta);
24+
}
25+
26+
public double cos(float theta) {
27+
theta = (float) (theta * 180 / Math.PI);
28+
return Math.cos(theta);
29+
}
30+
31+
public double tan(float theta) {
32+
theta = (float) (theta * 180 / Math.PI);
33+
return Math.tan(theta);
34+
}
35+
36+
public double cot(float theta) {
37+
theta = (float) (theta * 180 / Math.PI);
38+
return 1 / tan(theta);
39+
}
40+
}

src/section1/Main.java

Lines changed: 110 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,119 @@
1+
// Package path for class
12
package section1;
23

3-
public class Main {
4-
//Field
5-
private int number;
6-
protected int number2;
7-
int num3;
4+
// All classes you need to import
5+
import section2.Circle;
86

7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Scanner;
910

11+
// Class Deceleration
12+
public class Main {
1013
public static void main(String[] args) {
14+
// Object Instantiation
15+
Calculator calculator = new Calculator();
16+
// Method Call using dot(.)
17+
double result = calculator.division(5, 0);
18+
System.out.println("Result is : " + result);
19+
20+
// Convert String to Integer
21+
int num = Integer.parseInt("15");
22+
System.out.println("Your integer number is : " + num);
23+
24+
// Convert Integer to String
25+
String numString = String.valueOf(40);
26+
System.out.println("String of your Integer is : " + numString);
27+
28+
//Array Instantiation
29+
// Using array as List, Note you can't edit!
30+
int[] fixNumbers = new int[]{1, 2, 3, 4, 5};
31+
char[] charArray = new char[255];
32+
Person[] persons = new Person[5];
33+
persons[0] = new Person("Ali", 20);
34+
persons[2] = new Person("Sara", 18);
35+
persons[1] = new Person("Saeed", 18);
36+
persons[3] = new Person("Zahra", 23);
37+
persons[4] = new Person("Nasim", 21);
38+
39+
// In the tag (<>) Most Insert a Object Like Integer not int (and other primitive type) !!
40+
//List<int> wrongList = new ArrayList<>();
41+
List<Integer> integers = new ArrayList<>();
42+
integers.add(2);
43+
integers.add(3);
44+
integers.add(233);
45+
System.out.println(integers);
46+
// Remove by Object
47+
integers.remove(new Integer(2));
48+
System.out.println(integers);
49+
//Remove by index
50+
integers.remove(0);
51+
System.out.println(integers);
52+
53+
// Getting input from user using Scanner class, System.in means we get input from keyboard.
54+
System.out.println("PLEASE ENTER A SENTENCE:");
55+
Scanner scanner = new Scanner(System.in);
56+
// nextLine Method return Write in Consul
57+
String input = scanner.nextLine();
58+
System.out.println("Your Input is : " + input);
59+
60+
// PROGRAM FLOW
61+
// if example
62+
boolean flag = false;
63+
if (flag) {
64+
System.out.println("flag is TRUE");
65+
}
66+
else {
67+
System.out.println("flag is FALSE");
68+
}
69+
70+
// for example
71+
int counter = 0;
72+
for (int i = 0 ; i < 10 ; i++) {
73+
counter++;
74+
}
75+
System.out.println("Counter : " + counter);
76+
77+
// while example
78+
boolean condition = false;
79+
while (condition) {
80+
//Instruction
81+
System.out.println("IN WHILE");
82+
}
83+
System.out.println("While Condition is False");
84+
85+
// for each example
86+
for (Person p : persons) {
87+
p.sayHello();
88+
}
89+
90+
// Switch case example
91+
System.out.println("PLEASE ENTER A NUMBER BETWEEN 1 - 5");
92+
int in = scanner.nextInt();
93+
switch (in) {
94+
case 1:
95+
System.out.println("Your Input is One");
96+
break;
97+
case 2:
98+
System.out.println("Your Input is Two");
99+
break;
100+
case 3:
101+
System.out.println("Your Input is Three");
102+
break;
103+
case 4:
104+
System.out.println("Your Input is Four");
105+
break;
106+
case 5:
107+
System.out.println("Your Input is Five");
108+
break;
109+
default:
110+
System.out.println("Your Input is out of range");
111+
}
11112

113+
// Inheritance and Polymorphism (Method Overriding)
114+
int radios = 5;
115+
Circle circle = new Circle(radios);
116+
System.out.println("Area of circle with radios " + radios + " is " + circle.calArea());
12117
}
13118

14119
}

src/section1/Person.java

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,54 @@
11
package section1;
22

3-
import java.util.ArrayDeque;
4-
import java.util.ArrayList;
5-
import java.util.List;
6-
import java.util.Queue;
3+
// Import other class that we need, here!
74

8-
public class Person {
5+
public class Person implements talking, moving {
96
private String name;
107
private int age;
11-
// public static String nationality;
12-
// public final static String className = "Person";
138

14-
// Constructor 1
9+
// Static & final Keyword
10+
public final static String className = "Person";
11+
12+
// Constructor 1 : 'this' keyword point to current class
1513
public Person(String name, int age) {
1614
this.name = name;
1715
this.age = age;
1816
}
1917

20-
// Constructor 2
21-
public Person(int age) {
22-
this.age = age;
18+
// Constructor 2 : 'this' keyword point to current class
19+
public Person(String name) {
20+
this.name = name;
2321
}
2422

23+
// Sample Method for renaming person
24+
public void rename(String newName) {
25+
this.name = newName;
26+
}
27+
28+
// + in String Meaning to Concatenation of substring
2529
@Override
2630
public String toString() {
2731
return "name: " + name;
2832
}
2933

34+
// All Methods Below are defined in interfaces and Implementation's is here
35+
@Override
3036
public void sayHello() {
31-
System.out.println("Hi I'm " + this.name);
37+
System.out.println("Hi I'm " + name);
3238
}
3339

34-
public static void main(String[] args) {
35-
Person a = new Person("ali", 2);
36-
Person b = new Person("ali", 2);
37-
Person c = new Person("ali", 2);
38-
// Person b = new Person(2);
39-
// a = b;
40-
// Person.className = "sadsad";
41-
// System.out.println(Person.className);
42-
Person ali = new Person("ali", 4);
43-
List<Person> people = new ArrayList<>();
44-
Queue<Person> queue = new ArrayDeque<>();
45-
// people.add(new Person(4));
46-
// people.remove(0);
47-
// people.remove(ali);
48-
40+
@Override
41+
public void saySomething(String something) {
42+
System.out.println(something);
43+
}
4944

45+
@Override
46+
public void move(int dx) {
47+
System.out.println("I'm walking " + dx + " meters");
48+
}
5049

50+
@Override
51+
public void run(int v) {
52+
System.out.println("I'm running " + v + " m/s");
5153
}
5254
}

src/section1/moving.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package section1;
2+
3+
public interface moving {
4+
5+
void move(int dx);
6+
7+
void run(int v);
8+
}

src/section1/talking.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package section1;
2+
3+
public interface talking {
4+
5+
void sayHello();
6+
7+
void saySomething(String something);
8+
9+
}

src/section2/Circle.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package section2;
2+
3+
public class Circle extends Shape {
4+
private int r;
5+
6+
public Circle(int r) {
7+
this.r = r;
8+
}
9+
10+
@Override
11+
public double calArea() {
12+
area = Math.PI * r * r;
13+
return area;
14+
}
15+
16+
@Override
17+
public double calEnv() {
18+
env = 2 * Math.PI * r;
19+
return env;
20+
}
21+
}

src/section2/Rectangle.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package section2;
2+
3+
public class Rectangle extends Shape {
4+
private float width;
5+
private float height;
6+
7+
public Rectangle(float width, float height) {
8+
this.width = width;
9+
this.height = height;
10+
}
11+
12+
@Override
13+
public double calArea() {
14+
area = width * height;
15+
return area;
16+
}
17+
18+
@Override
19+
public double calEnv() {
20+
env = 2 * (width + height);
21+
return env;
22+
}
23+
}

src/section2/Shape.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package section2;
2+
3+
public abstract class Shape {
4+
5+
protected double area;
6+
protected double env;
7+
8+
// abstract methods for implementation in other formation
9+
public abstract double calArea();
10+
11+
public abstract double calEnv();
12+
13+
@Override
14+
public String toString() {
15+
return "Shape class";
16+
}
17+
}

0 commit comments

Comments
 (0)
0