Tuesday, June 2, 2015
DrivingTest Project
public class Demo {
public static void main(String[] args) {
Car myCar = new Car();
myCar.fuelCapacity = 20;
myCar.amountOfFuel = 10 ;
myCar.addGas(5);
System.out.println(myCar.amountOfFuel);
myCar.mileage = 15;
myCar.drive(100);
}
}
class Car{
// first think of properties which is nown and then actions which is verb
double mileage;
double amountOfFuel;
double fuelCapacity;
public void drive(double distance){
double fuelNeeded = distance/mileage;
if (amountOfFuel >= fuelNeeded) {
amountOfFuel = amountOfFuel - fuelNeeded;
System.out.println("You travelled "+ distance+" KM");
} else {
System.out.println("You dont have sufficient amount of fuel for the trip");
}
}
public void addGas(double amt){
double emptySpace = fuelCapacity - amountOfFuel;
if (amt < emptySpace) {
amountOfFuel = amountOfFuel + amt;
System.out.println(amt + " liters is added successfully");
} else {
amountOfFuel = fuelCapacity;
System.out.println("Tank is full");
}
}
public double getFuelLevel(){
System.out.println(amountOfFuel);
return amountOfFuel;
}
}
Class
DEMO1:
// CLASS: Class Defines the blueprint for set of object of the same type
// OBJECT: Each object is unique real and has properties and action
public class ClassDemo1 {
public static void main(String[] args) {
Circle obj = new Circle();
obj.radius = 2.22;
obj.area = 3.14 * obj.radius * obj.radius ;
System.out.println("Obj1 area is: "+obj.area);
Circle obj2 = new Circle();
obj2.radius = 5.55;
obj2.area = 3.14 *obj2.radius*obj2.radius;
System.out.println("Obj2 area is: "+obj2.area);
}
}
class Circle{
double radius ;
double area ;
}
DEMO2:
public class ClassDemo2 {
public static void main(String[] args) {
CircleClass obj1 = new CircleClass();
obj1.findArea(2.22);
CircleClass obj2 = new CircleClass();
obj2.findArea(5.55);
}
}
class CircleClass{
double radius;
double area;
public void findArea(double radius) {
this.radius = radius;
area = 3.14 * this.radius * this.radius ;
System.out.println("The area is: "+area);
}
}
Object
Properties and Action:
Object:
// Object is the combination of properties and actions
// Properties is noun and Actions are Verb
// CLASS: Class Defines the blueprint for set of object of the same type
// OBJECT: Each object is unique real and has properties and action
// obj1 and obj2 which is the object reference hold different space in the memory.
// object is made by calling the constructor
public class ObjectDemo {
public static void main(String[] args) {
CircleClass obj1 = new CircleClass();
obj1.findArea(2.22);
CircleClass obj2 = new CircleClass();
obj2.findArea(5.55);
}
}
class CircleClass{
double radius;
double area;
public void findArea(double radius) {
this.radius = radius;
area = 3.14 * this.radius * this.radius ;
System.out.println("The area is: "+area);
}
}
Object:
// Object is the combination of properties and actions
// Properties is noun and Actions are Verb
// CLASS: Class Defines the blueprint for set of object of the same type
// OBJECT: Each object is unique real and has properties and action
// obj1 and obj2 which is the object reference hold different space in the memory.
// object is made by calling the constructor
public class ObjectDemo {
public static void main(String[] args) {
CircleClass obj1 = new CircleClass();
obj1.findArea(2.22);
CircleClass obj2 = new CircleClass();
obj2.findArea(5.55);
}
}
class CircleClass{
double radius;
double area;
public void findArea(double radius) {
this.radius = radius;
area = 3.14 * this.radius * this.radius ;
System.out.println("The area is: "+area);
}
}
Method Overloading
// Methods within the same class that have the same name but slightly different characteristics
// like different number of parameters , different type of parameters that performed the related
// operations are called overloaded methods
// Overloaded method causes ambiquous invocation lead to compile error
public class MethodOverloadingDemo {
public static void main(String[] args) {
Maths math = new Maths();
double res = math.max(2.0, 4.0);
System.out.println(res);
int resu = math.max(2, 4);
System.out.println(resu);
double resul = math.max(2, 4.0);
System.out.println(resul);
}
}
class Maths{
public int max(int x , int y) {
int maximum;
if ( x > y) {
maximum = x;
}else{
maximum = y;
}
System.out.println("\n integer version is called");
return maximum;
}
public double max(double x , double y) {
double maximum;
if ( x > y) {
maximum = x;
}else{
maximum = y;
}
System.out.println("\n double version is called");
return maximum;
}
}
Monday, June 1, 2015
Method CallStack
------------------------------------------------------------------------------------------------------------------------
package packageName;
public class MethodCallStackDemo {
public static void main(String[] args) {
int maximum = max(5, 10);
System.out.println("Maximum between two no is: "+maximum);
int minimum = min(5, 10);
System.out.println("Minimum between two no is: "+minimum);
}
public static int max(int x , int y) {
int maximum;
if (x > y) {
maximum = x ;
} else {
maximum = y;
}
return maximum;
}
public static int min(int x, int y) {
int minimum;
if (x < y) {
minimum = x;
}else{
minimum = y;
}
return minimum;
}
}
Methods
// When to make the methods?
// Is that the single operation you want to do
// Does it needs several steps to do that exact operations
public class MethodDemo1 {
public static void main(String[] args) {
add();
add(20, 30);
int res = add(30, 40, 50);
System.out.println("SmartMethod: "+res);
}
// DUMB METHOD: When calling this method it always add 10 and 20 and
// print the result in console and do not return anything
public static void add() {
int a=10,b=20;
int result = a + b;
System.out.println("DumMethod: "+result);
}
// CLEVER METHOD: When calling this method it ask user to what numbers
// are to add and print the result in console and do not return anything
public static void add(int a, int b){
int result = a + b;
System.out.println("CleverMethod: "+result);
}
// SMART METHOD: When calling this method it ask user to what numbers are
// to add and it return the int value which is first not shown in the console and
// it can later on put in certain int variable and display as an output using syso
public static int add(int a , int b , int c){
int result = a + b+ c;
return result;
}
}
Subscribe to:
Posts (Atom)












