-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from MLSA-Mehran-UET/main
readd
- Loading branch information
Showing
24 changed files
with
950 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
public class IfAllConditionsExample { | ||
public static void main(String[] args) { | ||
int day = 5; | ||
int month = 10; | ||
int year= 2020; | ||
|
||
if (year >= 2020) { | ||
System.out.println("The year is greater or equal to 2020"); | ||
} else { | ||
System.out.println("The year is lower than 2020"); | ||
} | ||
|
||
if (month >= 11) { | ||
System.out.println("It's a winter month"); | ||
} else { | ||
System.out.println("It's not a winter month"); | ||
} | ||
|
||
if (day == 1) { | ||
System.out.println("It's Sunday"); | ||
} else if(day > 1 && day <= 6) { | ||
System.out.println("It's a work day"); | ||
} else { | ||
System.out.println("It's Saturday"); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,32 @@ | ||
# Java HEllo world Program | ||
# Hello World | ||
|
||
<p align="center"> | ||
<img src="https://miro.medium.com/max/512/1*jB76MLZjiNhGSQQvxm7LSQ.gif"> | ||
</p> | ||
|
||
Often the very first program written by someone who is learning to code, HelloWorld.java is a simple Java program that displays the message ```Hello World``` on the screen. | ||
|
||
In Java, every line of code that can actually run needs to be inside a class. | ||
|
||
The line: | ||
```java | ||
public class Helloworld{ | ||
... | ||
} | ||
``` | ||
declares that a new class with the name ```Helloworld``` is being defined that is **public**, meaning that any other class can access it. | ||
|
||
The next line | ||
```java | ||
public static void main(String[] args){ | ||
... | ||
} | ||
``` | ||
declares the main method and every application in Java must contain a main method. | ||
|
||
This line of code: | ||
```java | ||
System.out.println("Hello World"); | ||
``` | ||
|
||
which occurs inside the main method, uses the built-in println() method to print the string ```Hello World``` that is inside quotation marks on the screen. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class AnimalAbstraction { | ||
|
||
public static void main(String[] args) { | ||
List<Animal> zoo = new ArrayList<>(); | ||
zoo.add(new Snake()); | ||
zoo.add(new Elephant()); | ||
zoo.add(new Lion()); | ||
zoo.add(new Bear()); | ||
|
||
System.out.println("Welcome to the Zoo where you can hear all the animals"); | ||
for (Animal animal:zoo) { | ||
animal.makeNoise(); | ||
animal.sleep(); | ||
} | ||
} | ||
} | ||
|
||
abstract class Animal { | ||
public abstract void makeNoise(); | ||
public void sleep() { | ||
System.out.println("Zzzzz"); | ||
} | ||
} | ||
|
||
class Snake extends Animal { | ||
@Override | ||
public void makeNoise() { | ||
System.out.println("Snake says Hissssssss"); | ||
} | ||
} | ||
|
||
class Elephant extends Animal { | ||
@Override | ||
public void makeNoise() { | ||
System.out.println("Elephant says Pawoo"); | ||
} | ||
} | ||
|
||
class Lion extends Animal { | ||
@Override | ||
public void makeNoise() { | ||
System.out.println("Lion says Roarrr"); | ||
} | ||
} | ||
|
||
class Bear extends Animal { | ||
@Override | ||
public void makeNoise() { | ||
System.out.println("Bear says Growl"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,15 @@ | ||
# Abstraction in Java | ||
# Abstraction in Java | ||
Data abstraction is the process of hiding certain details and showing only essential information to the user. | ||
Abstraction can be achieved with either abstract classes or interfaces. | ||
|
||
The abstract keyword is a non-access modifier, used for classes and methods: | ||
- Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). | ||
- Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from). | ||
|
||
**Abstraction is used in Java to achieve security- hide certain details and display only the important details of an object.** | ||
|
||
Another way to achieve abstraction in Java, is with interfaces. | ||
|
||
An interface is a completely "abstract class" that is used to group related methods with empty bodies. | ||
|
||
To access the interface methods, the interface must be "implemented" (kinda like inherited) by another class with the `implements` keyword (instead of extends). The body of the interface method is provided by the "implement" class. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
public class Example2 { | ||
|
||
public static void main(String args[]) { | ||
int daysInMonth[] = new int[12]; | ||
|
||
daysInMonth[0] = 31; | ||
daysInMonth[1] = 28; | ||
daysInMonth[2] = 31; | ||
daysInMonth[3] = 30; | ||
daysInMonth[4] = 31; | ||
daysInMonth[5] = 30; | ||
daysInMonth[6] = 31; | ||
daysInMonth[8] = 30; | ||
daysInMonth[9] = 31; | ||
daysInMonth[10] = 30; | ||
daysInMonth[11] = 31; | ||
|
||
System.out.println("October has " + daysInMonth[10] + " days."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,23 @@ | ||
# Java Arrays | ||
# Java Arrays | ||
|
||
An **array** is a data structure that consists of a group of elements. Typically the elements will be of the same data type, such as a *string* or an *integer*. Arrays are commonly used to organise data so that a related set of values can be easily sorted or searched. | ||
|
||
As an example, an online shopping cart may use an array to store items selected by the user for purchase. When viewing the cart, the program will output one element of the array at a time. While the program could create a new variable for each result found, storing the results in an array is much more efficient way to programme and manage memory. | ||
|
||
To declare an array, define the variable type with square brackets: | ||
|
||
String[] products; | ||
|
||
Once declared, this variable now holds an array of strings. To add values to it, we can use what is known as an array literal - put the values in a comma-separated list, within curly braces: | ||
|
||
String[] products = {"Book", "Keyboard", "Toy", "Camera"}; | ||
|
||
Similarly, to create an array of integers, you could specify: | ||
|
||
int[] prices = {4, 12, 8, 40}; | ||
|
||
**Useful resources** | ||
|
||
[w3schools - arrays](https://www.w3schools.com/java/java_arrays.asp) | ||
|
||
[Oracle - Java arrays](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//Hierarchical Inheritance example | ||
class A | ||
{ | ||
public void methodA() | ||
{ | ||
System.out.println("method of Class A"); | ||
} | ||
} | ||
class B extends A | ||
{ | ||
public void methodB() | ||
{ | ||
System.out.println("method of Class B"); | ||
} | ||
} | ||
class C extends A | ||
{ | ||
public void methodC() | ||
{ | ||
System.out.println("method of Class C"); | ||
} | ||
} | ||
class D extends A | ||
{ | ||
public void methodD() | ||
{ | ||
System.out.println("method of Class D"); | ||
} | ||
} | ||
class HierarchicalInheritance | ||
{ | ||
public static void main(String args[]) | ||
{ | ||
B obj1 = new B(); | ||
C obj2 = new C(); | ||
D obj3 = new D(); | ||
//All classes can access the method of class A | ||
obj1.methodA(); | ||
obj2.methodA(); | ||
obj3.methodA(); | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//multilevel Inheritance example | ||
class Car{ | ||
public Car() | ||
{ | ||
System.out.println("Class Car"); | ||
} | ||
public void vehicleType() | ||
{ | ||
System.out.println("Vehicle Type: Car"); | ||
} | ||
} | ||
class Maruti extends Car{ | ||
public Maruti() | ||
{ | ||
System.out.println("Class Maruti"); | ||
} | ||
public void brand() | ||
{ | ||
System.out.println("Brand: Maruti"); | ||
} | ||
public void speed() | ||
{ | ||
System.out.println("Max: 90Kmph"); | ||
} | ||
} | ||
public class Maruti800 extends Maruti{ | ||
|
||
public Maruti800() | ||
{ | ||
System.out.println("Maruti Model: 800"); | ||
} | ||
public void speed() | ||
{ | ||
System.out.println("Max: 80Kmph"); | ||
} | ||
public static void main(String args[]) | ||
{ | ||
Maruti800 obj=new Maruti800(); | ||
obj.vehicleType(); | ||
obj.brand(); | ||
obj.speed(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Employee{ | ||
float salary=40000; | ||
} | ||
class Programmer extends Employee{ | ||
int bonus=10000; | ||
public static void main(String args[]){ | ||
Programmer p=new Programmer(); | ||
System.out.println("Programmer salary is:"+p.salary); | ||
System.out.println("Bonus of Programmer is:"+p.bonus); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,41 @@ | ||
# Inheritance in Java | ||
# Inheritance in Java | ||
|
||
WHAT IS INHERITANCE | ||
|
||
|
||
Inheritance can be defined as the process where one class inherits the properties(methods and fields) of another. | ||
The class which inherits is known as Sub class(derived class,child class) and the class which is inherited is called super-class(base class,parent class) | ||
|
||
|
||
TYPES OF INHERITANCE: | ||
|
||
There are three kind of inheritence | ||
Single Inheritance | ||
Multilevel Inheritance | ||
Heirarchical Inheritance | ||
|
||
Single Inheritance : | ||
|
||
|
||
Single inheritance can be defined as a derived class to inherit the basic methods (data members and variables) and behavior from a superclass. It's a basic is-a relationship concept exists here. Basically, java only uses a single inheritance as a subclass cannot extend more superclass | ||
|
||
Multiple Inheritance: | ||
|
||
|
||
The Java programming language supports multiple inheritance of type, which is the ability of a class to implement more than one interface. .As with multiple inheritance of implementation, a class can inherit different implementations of a method defined (as default or static) in the interfaces that it extends. | ||
|
||
Heirarchical Inheritance: | ||
|
||
|
||
when a class has more than one child classes (sub classes) or in other words more than one child classes have the same parent class then this type of inheritance is known as hierarchical inheritance. | ||
|
||
KEYWORD FOR USE: | ||
|
||
|
||
extends is the keyword used to inherit the properties of a class | ||
|
||
WHY WE USE INTERITANCE: | ||
|
||
|
||
Inheritance allows us to reuse of code, it improves reusability in your java application. | ||
For Method Overriding (so runtime polymorphism can be achieved). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//Single Inheritance example | ||
class Animal{ | ||
void eat(){System.out.println("eating...");} | ||
} | ||
class Dog extends Animal{ | ||
void bark(){System.out.println("barking...");} | ||
} | ||
class SingleInheritance{ | ||
public static void main(String args[]){ | ||
Dog d=new Dog(); | ||
d.bark(); | ||
d.eat(); | ||
}} |
Oops, something went wrong.