Added examples for java basics module

This commit is contained in:
Mayank Agarwal
2023-04-01 17:42:50 +05:30
parent 5f09e78f92
commit 8a571e9a39
6 changed files with 303 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
import java.util.*;
public class JavaAccessModifiers {
public static void main(String[] args) {
javaInstanceOfOperator();
}
public int myVariable = 10;
public void myMethod() {
// method code here
}
private int myVariable = 10;
private void myMethod() {
// method code here
}
protected int myVariable = 10;
protected void myMethod() {
// method code here
}
int myVariable = 10;
void myMethod() {
// method code here
}
}

View File

@@ -0,0 +1,71 @@
import java.util.*;
public class JavaConstructor {
public class Car {
private String make;
private String model;
private int year;
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
}
public class BankAccount {
private String accountNumber;
private String accountHolder;
private double balance;
public BankAccount(String accountNumber, String accountHolder, double balance) {
this.accountNumber = accountNumber;
this.accountHolder = accountHolder;
this.balance = balance;
}
public String getAccountNumber() {
return accountNumber;
}
public String getAccountHolder() {
return accountHolder;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
System.out.println(amount + " deposited to account " + accountNumber);
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println(amount + " withdrawn from account " + accountNumber);
} else {
System.out.println("Insufficient balance");
}
}
}
}

View File

@@ -0,0 +1,85 @@
import java.util.*;
public class JavaConstructor {
public static void main(String[] args) {
javaInstanceOfOperator();
}
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Person {
private String name;
private int age;
public Person() {
this.name = null;
this.age = 0;
}
}
public class Person {
private String name;
private int age;
public Person() {
this.name = "John Doe";
this.age = 30;
}
}
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Person {
private String name;
private int age;
public Person() {
this("John Doe", 30);
}
public Person(String name) {
this(name, 30);
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(Person other) {
this.name = other.name;
this.age = other.age;
}
}
}

View File

@@ -0,0 +1,25 @@
import java.util.*;
public class JavaThisKeyword {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
public int add(int a, int b, int c) {
return super.add(a, b) + c;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
int result1 = calc.add(2, 3);
double result2 = calc.add(2.5, 3.5);
System.out.println(result1);
System.out.println(result2);
}
}

View File

@@ -0,0 +1,36 @@
import java.util.*;
public class JavaAccessModifiers {
public static void main(String[] args) {
javaInstanceOfOperator();
printHello();
int sum = add(2, 3);
boolean result = isPositive(-5);
}
public void printHello() {
System.out.println("Hello");
}
public int add(int a, int b) {
return a + b;
}
public boolean isPositive(int num) {
return num > 0;
}
public int add(int a, int b) {
return a + b;
}
public boolean isPositive(int num) {
return num > 0;
}
}

View File

@@ -0,0 +1,52 @@
import java.util.*;
public class JavaThisKeyword {
public static void main(String[] args) {
javaInstanceOfOperator();
}
public class User {
private String username;
public User(String username) {
setUsername(username);
}
public void setUsername(String username) {
this.username = username;
}
}
public class Person {
private String name;
public void setName(String name) {
this.name = name;
}
public void printName(String localName) {
System.out.println("Local variable name: " + localName);
System.out.println("Instance variable name: " + this.name);
}
}
public class UserProfile {
private String username;
private int userAge;
public UserProfile() {
this("John Doe", 30);
}
public UserProfile(String name) {
this(name, 30);
}
public UserProfile(String name, int age) {
this.username = name;
this.userAge = age;
}
}
}