Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0269e04

Browse files
Add files via upload
1 parent d19b513 commit 0269e04

File tree

10 files changed

+348
-0
lines changed

10 files changed

+348
-0
lines changed

‎ClassAndObjects/Actor.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ClassAndObjects;
2+
3+
public class Actor {
4+
5+
// name is a Member Variable
6+
private String name;
7+
8+
public String getName() {
9+
return name;
10+
}
11+
12+
public void setName(String name) {
13+
// This is called shadowing
14+
// Local variable or parameter with
15+
// same name as a member variable
16+
// this.name refers to member variable
17+
// name refers to local variable
18+
this.name = name;
19+
}
20+
21+
public static void main(String[] args) {
22+
// bradPitt & tomCruise are objects or instances
23+
// of Class Actor
24+
// Each instance has separate value for the
25+
// member variable name
26+
Actor bradPitt = new Actor();
27+
bradPitt.setName("Brad Pitt");
28+
29+
Actor tomCruise = new Actor();
30+
tomCruise.setName("Tom Cruise");
31+
}
32+
}

‎ClassAndObjects/Bank.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package ClassAndObjects;
2+
3+
class BankAccount {
4+
int accountNum = 0;
5+
float accountBalance = 0;
6+
7+
public BankAccount() {
8+
}
9+
10+
public BankAccount(int accountNumber, float balance) {
11+
accountNum = accountNumber;
12+
accountBalance = balance;
13+
}
14+
15+
void setAccountNumber(int num) {
16+
accountNum = num;
17+
}
18+
19+
void withdraw(float amount) {
20+
accountBalance = accountBalance - amount;
21+
}
22+
23+
void deposit(float amount) {
24+
accountBalance = accountBalance + amount;
25+
}
26+
void status(){
27+
System.out.println("Account Number: "+accountNum);
28+
System.out.println("Account Balance: "+accountBalance);
29+
}
30+
}
31+
32+
public class Bank {
33+
34+
public static void main(String[] arg) {
35+
BankAccount account1 = new BankAccount();
36+
account1.setAccountNumber(456);
37+
account1.deposit(1000);
38+
account1.status();
39+
BankAccount account2 = new BankAccount(142, 2000);
40+
account2.withdraw(500);
41+
account2.status();
42+
43+
}
44+
}

‎ClassAndObjects/BicycleDemo.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package ClassAndObjects;
2+
3+
class Bicycle{
4+
int speed=0;
5+
int gear=1;
6+
void changeGear(int newValue){
7+
gear=newValue;
8+
}
9+
void speedUp(int inc){
10+
speed=speed+inc;
11+
}
12+
void applybreak(int dec){
13+
speed=speed-dec;
14+
}
15+
void printstate(){
16+
System.out.println("Speed : "+speed+"\t Gear : "+gear);
17+
}
18+
}
19+
20+
public class BicycleDemo {
21+
22+
public static void main(String[] args){
23+
Bicycle b1=new Bicycle();
24+
Bicycle b2=new Bicycle();
25+
26+
b1.speedUp(50);
27+
b1.changeGear(3);
28+
b1.applybreak(20);
29+
b1.printstate();
30+
31+
b2.speedUp(60);
32+
b2.changeGear(2);
33+
b2.printstate();
34+
}
35+
}

‎ClassAndObjects/CricketScorer.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ClassAndObjects;
2+
3+
public class CricketScorer {
4+
// Instance Variables - constitute the state of an object
5+
private int score;
6+
7+
// Behavior - all the methods that are part of the class
8+
// An object of this type has behavior based on the
9+
// methods four, six and getScore
10+
public void four() {
11+
score = score + 4;
12+
}
13+
14+
public void six() {
15+
score = score + 6;
16+
}
17+
18+
public int getScore() {
19+
return score;
20+
}
21+
22+
public static void main(String[] args) {
23+
CricketScorer scorer = new CricketScorer();
24+
scorer.six();
25+
// State of scorer is (score => 6)
26+
scorer.four();
27+
// State of scorer is (score => 10)
28+
System.out.println(scorer.getScore());
29+
}
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ClassAndObjects;
2+
3+
//Every Java class extends Object class
4+
public class EveryClassExtendsObject {
5+
public void testMethod() throws CloneNotSupportedException {
6+
// toString,hashCode,clone methods are
7+
// inherited from Object class
8+
System.out.println(this.toString());
9+
System.out.println(this.hashCode());
10+
System.out.println(this.clone());
11+
}
12+
13+
public static void main(String[] args) throws CloneNotSupportedException {
14+
EveryClassExtendsObject example1 = new EveryClassExtendsObject();
15+
16+
EveryClassExtendsObject example2 = new EveryClassExtendsObject();
17+
18+
if (example1 instanceof Object) {
19+
System.out.println("I extend Object");// Will be printed
20+
}
21+
22+
// equals method is inherited from Object class
23+
System.out.println(example1.equals(example2));// false
24+
25+
}
26+
}

‎ClassAndObjects/Monster.java‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package ClassAndObjects;
2+
3+
4+
class MonsterWorks {
5+
6+
public final String TOMBSTONE = "Here Lies a Dead monster";
7+
private int health = 500;
8+
private int attack = 20;
9+
private int movement = 2;
10+
private boolean alive = true;
11+
public String name = "Big Monster";
12+
13+
public int getAttack() {
14+
return attack;
15+
}
16+
17+
public int getMovement() {
18+
return movement;
19+
}
20+
21+
public int getHealth() {
22+
return health;
23+
}
24+
25+
public void setHealth(int decreaseHealth) {
26+
health = health - decreaseHealth;
27+
if (health < 0 && alive == true) {
28+
alive = false;
29+
}
30+
}
31+
32+
public void setHealth(double decreaseHealth) {
33+
int intDecreaseHealth = (int) decreaseHealth;
34+
health = health - intDecreaseHealth;
35+
if (health < 0) {
36+
alive = false;
37+
}
38+
}
39+
}
40+
//Moster Class
41+
public class Monster{
42+
43+
public static void main(String[] args){
44+
MonsterWorks gorilla = new MonsterWorks();
45+
gorilla.name = "Jumbo";
46+
System.out.println(gorilla.name+" got health power: "+gorilla.getHealth());
47+
System.out.println(gorilla.name + " has an attack value of " + gorilla.getAttack());
48+
gorilla.setHealth(30);
49+
System.out.println(gorilla.name+" got attacked, Now Health "+gorilla.getHealth());
50+
}
51+
}

‎ClassAndObjects/ObjectPass.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ClassAndObjects;
2+
3+
class PassObjRef {
4+
5+
int a, b;
6+
7+
public PassObjRef(int i, int j) {
8+
a = i;
9+
b = j;
10+
}
11+
12+
// pass an object
13+
// Java always pass object (call-by-reference)
14+
// reference of an object stored in stack area.
15+
void meth(PassObjRef o) {
16+
o.a *= 2;
17+
o.b /= 2;
18+
}
19+
}
20+
21+
class ObjectPass{
22+
public static void main(String args[]) {
23+
PassObjRef ob = new PassObjRef(15, 20);
24+
System.out.println("ob.a and ob.b before call: " + ob.a + " " + ob.b);
25+
ob.meth(ob);
26+
System.out.println("ob.a and ob.b after call: " + ob.a + " " + ob.b);
27+
}
28+
29+
}

‎ClassAndObjects/Radio.java‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package ClassAndObjects;
2+
3+
public class Radio {
4+
int volume;
5+
String station;
6+
boolean powerStatus;
7+
8+
public void increaseVolume() {
9+
volume+=2;
10+
}
11+
12+
public void decreaseVolume(){
13+
volume--;
14+
}
15+
16+
public void changeStation(String newStation) {
17+
station = newStation;
18+
}
19+
20+
public void turnOn() {
21+
volume = 3;
22+
powerStatus = true;
23+
}
24+
25+
public void turnOff() {
26+
volume = 0;
27+
station="Nil";
28+
powerStatus = false;
29+
}
30+
void status(){
31+
if(powerStatus)
32+
System.out.println("Radio is On! ");
33+
else
34+
System.out.println("Radio is off! ");
35+
System.out.println("Station Name: "+station);
36+
System.out.println("Volume: "+volume);
37+
38+
}
39+
public static void main(String[] args) {
40+
Radio r1=new Radio();
41+
r1.changeStation("Radio Mirchi! ");
42+
r1.turnOn();
43+
r1.status();
44+
System.out.println("----------------------------------");
45+
r1.increaseVolume();
46+
r1.status();
47+
System.out.println("----------------------------------");
48+
r1.decreaseVolume();
49+
r1.turnOff();
50+
r1.status();
51+
System.out.println("----------------------------------");
52+
53+
}
54+
}

‎ClassAndObjects/Solution.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ClassAndObjects;
2+
//public class Cricketer {
3+
// String name;
4+
// int odiRuns;
5+
// int testRuns;
6+
// int t20Runs;
7+
//
8+
// public int totalRuns() {
9+
// int totalRuns = odiRuns + testRuns + t20Runs;
10+
// return totalRuns;
11+
// }
12+
//}
13+
import java.util.Scanner;
14+
15+
public class Solution {
16+
17+
public static void main(String[] args) {
18+
Scanner scan = new Scanner(System.in);
19+
int i = scan.nextInt();
20+
double d = scan.nextDouble();
21+
scan.nextLine();// to consume extra line generated by inputBuffer in nextDouble
22+
String s = scan.nextLine();
23+
24+
System.out.println("String: " + s);
25+
System.out.println("Double: " + d);
26+
System.out.println("Int: " + i);
27+
scan.close();
28+
}
29+
}

‎ClassAndObjects/TrickyQue.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ClassAndObjects;
2+
3+
public class TrickyQue {
4+
5+
public static void main(String[] args) {
6+
Integer i1 = 127;
7+
8+
Integer i2 = 127;
9+
10+
System.out.println(i1 == i2);
11+
12+
Integer i3 = 128;
13+
14+
Integer i4 = 128;
15+
16+
System.out.println(i3 == i4);
17+
}
18+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /