Skip to content

Commit 68b4330

Browse files
committed
first commit
1 parent 394aa9b commit 68b4330

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

besic_constructor.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// CONSTRUCTOR: special method that is called when an object is instrantiated ( created )
2+
3+
public class besic_constructor{
4+
public static void main(String[] args){
5+
Human human1 = new Human("Sachin",22,70);
6+
Human human2 = new Human("shivam",22,65);
7+
8+
System.out.println("human1 name is : " + human1.name + " and age is " + human1.age );
9+
10+
System.out.println("human2 name is: " + human2.name + " and age is " + human2.age );
11+
12+
human1.eat();
13+
human2.drink();
14+
}
15+
}
16+
17+
class Human{
18+
String name;
19+
int age;
20+
double weight;
21+
22+
Human(String name, int age , double weight){
23+
this.name = name;
24+
this.age = age;
25+
this.weight = weight;
26+
}
27+
28+
void eat(){
29+
System.out.println( this.name + " is eating ");
30+
}
31+
32+
void drink(){
33+
System.out.println( this.name + " is drinking bear ");
34+
}
35+
}

besic_dice.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
3+
// LOCAL : declared inside a method visible only to that method
4+
// GLOBAL : declated outside a method but within a class visible to all part of a class
5+
6+
import java.util.Random;
7+
8+
public class besic_dice{
9+
public static void main(String[] args){
10+
11+
dice DiceRoll = new dice();
12+
13+
}
14+
}
15+
16+
// class dice{
17+
18+
// dice(){
19+
20+
// // this is local var example bcz random and number are visialbe only dice() ;
21+
// Random random = new Random();
22+
// int number = 0;
23+
24+
// Roll(random, number);
25+
// }
26+
27+
// void Roll(Random random, int number){
28+
// number = random.nextInt(6) + 1;
29+
// System.out.println(number);
30+
// }
31+
32+
// }
33+
34+
class dice{
35+
36+
// this is local var method
37+
Random random;
38+
int number;
39+
40+
dice(){
41+
random = new Random();
42+
Roll();
43+
}
44+
45+
void Roll(){
46+
number = random.nextInt(6) + 1;
47+
System.out.println(number);
48+
}
49+
}

overloaded_constructor.java

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// OVERLOADED CONSTRUCTORS: multiple constructors within a class with the same name,
2+
// but name different parameters
3+
// (name + parameter = signature)
4+
5+
public class overloaded_constructor{
6+
public static void main(String[] args){
7+
8+
// if all ingridints is need
9+
// Pizza pizza = new Pizza("tomato","mozerilla","garlic","onion");
10+
11+
12+
// if only three need
13+
// Pizza pizza = new Pizza("tomato","mozerilla","garlic");
14+
15+
// if only two need
16+
// Pizza pizza = new Pizza("tomato","mozerilla");
17+
18+
// if only one need
19+
// Pizza pizza = new Pizza("tomato");
20+
21+
//if plan pizz is need
22+
Pizza pizza = new Pizza();
23+
24+
25+
System.out.println("here your pizza type: ");
26+
System.out.println(pizza.souce);
27+
System.out.println(pizza.cheese);
28+
System.out.println(pizza.bread);
29+
System.out.println(pizza.type);
30+
}
31+
}
32+
33+
class Pizza{
34+
35+
36+
String souce;
37+
String cheese;
38+
String bread;
39+
String type;
40+
41+
//if plan pizza is need
42+
Pizza(){
43+
}
44+
45+
// if only one item is need
46+
Pizza(String souce ){
47+
this.souce = souce;
48+
}
49+
50+
51+
// if only two item needs
52+
Pizza(String souce, String cheese ){
53+
this.souce = souce;
54+
this.cheese = cheese;
55+
}
56+
57+
// if only three item needs
58+
Pizza(String souce, String cheese , String bread ){
59+
this.souce = souce;
60+
this.cheese = cheese;
61+
this.bread = bread;
62+
}
63+
64+
// if all item needs
65+
Pizza(String souce, String cheese , String bread , String type){
66+
this.souce = souce;
67+
this.cheese = cheese;
68+
this.bread = bread;
69+
this.type = type;
70+
}
71+
72+
73+
74+
}

0 commit comments

Comments
 (0)