Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added Oops concept and their program. #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions DataStructureAndAlgorithms/src/OOPs In Java/BMW.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@


public class BMW extends car {

public String getCompany() {
return "BMW";
}
}
32 changes: 32 additions & 0 deletions DataStructureAndAlgorithms/src/OOPs In Java/car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// inheritance : inherited form vehicle class



public abstract class car extends vehicle{
int numGears;
boolean isConvertible;

public car() {
super(100);
}

public car(int numGears,int maxSpeed) {
super(maxSpeed);
this.numGears = numGears;
System.out.println("car");
}

public boolean isMoterized() {
return true;
}


public void print() {
//System.out.println("speed of car : " + maxSpeed);
super.print();
System.out.println("num of Gears in car : " + numGears);
System.out.println("car's convertible feature : " + isConvertible);

}

}
13 changes: 13 additions & 0 deletions DataStructureAndAlgorithms/src/OOPs In Java/car2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@


public class car2 implements vehicle2 {
private int speed;

public int getSpeed() {
return speed;
}

public void setSpeed(int speed) {
this.speed = speed;
}
}
44 changes: 44 additions & 0 deletions DataStructureAndAlgorithms/src/OOPs In Java/carUse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@


public class carUse {

public static void main(String[] args) {

vehicle v = new BMW();
v.color = "red";
//v.maxSpeed = 180;
v.setmaxSpeed(100);
// System.out.println(v.color);
// System.out.println(v.getmaxSpeed());
// v.print();


car c = new BMW();
// c.print();

c.color = "black";
//c.maxSpeed = 180;
c.setmaxSpeed(180);
c.numGears = 5;
c.isConvertible = true;

System.out.println(c.color);
//System.out.println(c.maxSpeed);
System.out.println(c.getmaxSpeed());
System.out.println(c.numGears);
System.out.println(c.isConvertible);
System.out.println(c.getCompany());

c.print();

vehicle v2 = new BMW();

v2.color = "red";
//v2.numGears; //error

vehicle2 v3 = new car2();
v3.setSpeed(9384757);
System.out.println(v3.getSpeed());
}

}
75 changes: 75 additions & 0 deletions DataStructureAndAlgorithms/src/OOPs In Java/fraction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@


public class fraction {
private int numerator;
private int denominator;

public fraction(int numerator, int denominator) {
this.numerator = numerator;
// if(denominator == 0 )
// // error out
this.denominator = denominator;

simplify();
}

public void setNumerator(int numerator){
this.numerator = numerator;
simplify();
}

public void setDenominator(int denominator) {
// if(denominator == 0)
// // error out
this.denominator = denominator;
simplify();
}

public int getNumerator() {
return numerator;
}

public int getDenominator() {
return denominator;
}

private void simplify() {
int gcd = 1;

int smaller = Math.min(numerator, denominator);
for(int i=2; i<=smaller; i++) {
if((numerator%i == 0) && (denominator%i == 0))
gcd = i;
}
numerator = numerator/gcd;
denominator = denominator/gcd ;
}

public void print() {
simplify();
if(denominator == 1)
System.out.println(numerator);
else
System.out.println(numerator + "/" + denominator);
}

public void add(fraction f) {
this.numerator = this.numerator*f.denominator + this.denominator*f.numerator;
this.denominator = this.denominator*f.denominator;
simplify();
}

public void multiply(fraction f) {
this.numerator *= f.numerator;
this.denominator *= f.denominator;
simplify();
}

public static fraction add(fraction f1, fraction f2) {
int a = f1.numerator*f2.denominator + f1.denominator*f2.numerator;
int b = f1.denominator*f2.denominator;
fraction f = new fraction(a,b);
return f;
}

}
34 changes: 34 additions & 0 deletions DataStructureAndAlgorithms/src/OOPs In Java/fractionUse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@



public class fractionUse {

public static void main(String[] args) {
fraction f1 = new fraction(50, 40);
fraction f2 = new fraction(30, 20);
fraction f3 = new fraction(8, 6);
f1.print();
f2.print();
f3.print();

System.out.println(f1.getNumerator());
System.out.println(f1.getDenominator());

f2.setNumerator(1);
f2.setDenominator(2);
f2.print();

f1.add(f2);

f1.print();

f2.multiply(f3);
f2.print();

f3 = fraction.add(f1,f2);

f3.print();

}

}
33 changes: 33 additions & 0 deletions DataStructureAndAlgorithms/src/OOPs In Java/student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@


public class student {
public String name;
public int rollNo;
private int age;
private static int numStudent; // static member is same for all objects;

public student() { // default constructor
numStudent++;
}
public student(String name, int rollNo, int age) {
this.name = name;
this.rollNo = rollNo;
this.age = age;
numStudent++;
}

public void setAge(int age) {
if(age<16) // this is the actual use of set function
return; //that control of setting data is in your hand

this.age = age;
}

public int getAge() {
return this.age;
}

public static int getNumStudent() {
return numStudent;
}
}
47 changes: 47 additions & 0 deletions DataStructureAndAlgorithms/src/OOPs In Java/studentUse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@


public class studentUse {

public static void main(String[] args) {
student s1 = new student();
student s2 = new student();


s1.name = "Ajay";
s1.rollNo = 15;
//s1.age = 23; note allowed
s1.setAge(12); // returns 0;
s1.setAge(23);


s2.name = "sachin";
s2.rollNo = 131;
// s2.age = 21; not allowed
s2.setAge(21);

System.out.println(s1.name);
System.out.println(s1.rollNo);
//System.out.println(s1.age); // not allowed
System.out.println(s1.getAge());

System.out.println(s2.name);
System.out.println(s2.rollNo);
//System.out.println(s2.age); // not allowed
System.out.println(s2.getAge());



// constructor use

student s3 = new student("nayan", 190,21);

System.out.println(s3.name);
System.out.println(s3.rollNo);
System.out.println(s3.getAge());

System.out.println(s1.getNumStudent() + " " + s2.getNumStudent() + " " + s3.getNumStudent() + " " + student.getNumStudent()); // all have same value;


}

}
28 changes: 28 additions & 0 deletions DataStructureAndAlgorithms/src/OOPs In Java/vehicle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@


public abstract class vehicle {
protected String color;
private int maxSpeed;

public vehicle(int maxSpeed) {
this.maxSpeed = maxSpeed;
System.out.println("vehicle");
}


public int getmaxSpeed() {
return maxSpeed;
}
public void setmaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}

public abstract String getCompany();

public abstract boolean isMoterized();

public void print() {
System.out.println("color of vehicle : " + color);
System.out.println("speed of vehicle : " + maxSpeed);
}
}
6 changes: 6 additions & 0 deletions DataStructureAndAlgorithms/src/OOPs In Java/vehicle2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


public interface vehicle2 {
public int getSpeed();
public void setSpeed(int speed);
}