Merge pull request #68 from mayank4all/master

march articles code review
This commit is contained in:
javadevjournal
2023-04-27 07:49:00 -07:00
committed by GitHub
8 changed files with 371 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,26 @@
import java.util.*;
public class JavaContinue {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
if (i == 5) {
break;
}
System.out.println(i);
i++;
}
}
public static void breakExample(String[] args) {
int[][] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
outer: for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
if (numbers[i][j] == 5) {
System.out.println("Found 5 at index [" + i + "][" + j + "]");
break outer;
}
}
}
}
}

View File

@@ -0,0 +1,42 @@
import java.util.*;
public class JavaFinal {
public static void main(String[] args) {
javaInstanceOfOperator();
}
public static void javaInstanceOfOperator() {
String str = "Hello, World!";
boolean result = str instanceof String;
System.out.println(result);
}
class Animal {}
class Dog extends Animal {}
public void checkInstance() {
Animal animal = new Dog();
if (animal instanceof Dog) {
Dog dog = (Dog) animal;
System.out.println("Woof!");
}
}
public void javaInstanceOfInterface() {
Thread thread = new Thread();
if (thread instanceof Runnable) {
System.out.println("Thread implements Runnable");
}
}
public void typeCastingWithInstanceOf() {
Object animal = new Cat();
if (animal instanceof Dog) {
Dog dog = (Dog) animal;
dog.bark();
} else if (animal instanceof Cat) {
Cat cat = (Cat) animal;
cat.meow();
}
}
}

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;
}
}
}