JAVA Class Notes
JAVA Class Notes
//////////////////////////////////////////////////////////////////////////////////////
public class MyClass {
public static void main(String[ ] args) {
Person j= new Person("John");
j.setAge(20); /////referenced type - 21
celebrateBirthday(j);
System.out.println(j.getAge());
int x=5;
celebrate(x); //////value type - 5
System.out.println(x);
}
static public void celebrate(int y)
{
y=y+1;
}
static void celebrateBirthday(Person p) {
p.setAge(p.getAge() + 1);
}
}
public class Person {
private String name;
private int age;
Person (String n) {
this.name = n;
}
//////////////////////////////////////////////////////////////////////////////////////
//be attentive to access modifiers
class Standard {
protected void draw() {
System.out.println("Drawing");
}
protected void write() {
System.out.println("Writing");
}
}
//fix the class
class Pro extends Standard{
protected void useEffects() {
System.out.println("Using Effects");
}
protected void changeResolution() {
System.out.println("Changing Resolution");
}
}
public class Main {
public static void main(String[] args) {
Standard standard1 = new Standard();
Pro pro1 = new Pro();
//standard version
standard1.draw();
standard1.write();
//Pro version
pro1.draw();
pro1.write();
pro1.useEffects();
pro1.changeResolution();
}
}
example for inheritance
//////////////////////////////////////////////////////////////////////////////////////
class A {
int a = 100;
public A() {
System.out.println("New A");
}
public void a(){
System.out.println("A");
}
}
class B extends A {
int b = 200;
public B() {
super.a=50;
System.out.println("New B");
}
public void b(){
super.a = 10;
System.out.println("B");
}
}
class Program {
public static void main(String[ ] args) {
B obj = new B();
System.out.println(obj.a);
obj.b();
System.out.println(obj.a);
}
}
super keyword
//////////////////////////////////////////////////////////////////////////////////////
class Animal {
public void makeSound() {
System.out.println("Grr...");
}
}
class Cat extends Animal {
public void makeSound() {
System.out.println("Meow");
}
}
class Dog extends Animal {
public void makeSound() {
System.out.println("Woof");
}
}
class Program {
public static void main(String args[ ]) {
/*POLYMORPHISM*/
Animal a = new Dog();
Animal b = new Cat();
a.makeSound();
b.makeSound();
/*INHERITANCE*/
Dog a = new Dog();
Cat b = new Cat();
a.makeSound();
b.makeSound();
}
}
inheritance vs polymorphism
//////////////////////////////////////////////////////////////////////////////////////
abstract class Animal {
int legs = 0;
abstract void makeSound();
}
class Cat extends Animal {
public void makeSound() {
System.out.println("Meow");
}
}
public class Program {
public static void main(String[] args) {
Cat c = new Cat();
c.makeSound();
}
}
//////////////////////////////////////////////////////////////////////////////////////
Animal a = new Cat(); //upcasting done automatically
((Cat)a).makeSound(); //downcasting is done manually
--------------------------------------------------------------
A obj1 = new B();
B obj2 = (B) obj1;
obj2.print();
Casting
//////////////////////////////////////////////////////////////////////////////////////
throws throw
throw new **Exception() or throw new **Exception("a message");
---------------------------
try{
}
catch(**Exception e1){
}
catch(**Exception e2){
}
catch(Exception e){
}
------------------------------------
//////////////////////////////////////////////////////////////////////////////////////
inheritance another example
Thread
//////////////////////////////////////////////////////////////////////////////////////
import java.util.ArrayList;
public class MyClass {
public static void main(String[ ] args) {
ArrayList<String> colors = new ArrayList<String>();
colors.add("Red");
colors.add("Blue");
colors.add("Green");
colors.add("Orange");
colors.remove("Green");
System.out.println(colors);
System.out.println(colors.get(1));
if(colors.contains("Red"))
System.out.println("Present");
System.out.println(colors.size());
colors.clear();
System.out.println(colors);
}
}
----------------------------------------------------------------
import java.util.ArrayList;
public class MyClass {
public static void main(String[ ] args) {
ArrayList colors = new ArrayList();
colors.add("Red");
colors.add("Blue");
colors.add("Green");
colors.add(3);//--if type not declared,like this can be added to
linkedlist,hashset
colors.remove("Green");
System.out.println(colors);
System.out.println(colors.get(1));
if(colors.contains("Red"))
System.out.println("Present");
if(colors.contains(3))
System.out.println("Present");
System.out.println(colors.size());
colors.clear();
System.out.println(colors);
}
}
//////////////////////////////////////////////////////////////////////////////////////
import java.util.Scanner;
import java.util.LinkedList;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
LinkedList<String> words = new LinkedList<String>();
// while(words.size()!=5)
// {
// String xyz = sc.next();
// words.add(xyz);
// }
for(int i=0;words.size()!=5;i++)
{
String xyz = sc.next();
words.add(xyz);
}
for(int i=0;i<words.size();i++)
{
String str = words.get(i);
int count = str.length(); // or int count =
words.get(i).length();
if(count>4)
System.out.println(words.get(i));
}
}
}
//////////////////////////////////////////////////////////////////////////////////////
import java.util.HashMap;
public class MyClass {
public static void main(String[ ] args) {
HashMap<String, Integer> points = new HashMap<String, Integer>();
points.put("Amy", 154);
points.put("Dave", 42);
points.put("Rob", 733);
System.out.println(points);
System.out.println(points.keySet());
System.out.println(points.get("Davea"));
points.remove("Rob");
System.out.println(points);
points.put("Amy", 1000);
System.out.println(points); //dont use same key orelse overrides
org val
if(points.containsKey("Dave"))
System.out.println("Present");
if(points.containsValue(42))
System.out.println("Present");
}
}
//////////////////////////////////////////////////////////////////////////////////////
import java.util.HashMap;
public class MyClass {
public static void main(String[ ] args) {
HashMap<String, Integer> points = new HashMap<String, Integer>();
points.put("Amy", 154);
points.put("Dave", 42);
points.put("Rob", 733);
System.out.println(points);
System.out.println(points.keySet());
System.out.println(points.get("Davea"));
points.remove("Rob");
System.out.println(points);
points.put("Amy", 1000);
System.out.println(points); //dont use same key orelse overrides org val
if(points.containsKey("Dave"))
System.out.println("Present");
if(points.containsValue(42))
System.out.println("Present");
points.clear();
System.out.println(points);
}
}
HASHMAP/HASH MAP-
put,remove,get,size,clear,containsKey,containsValue,keySet()
-----------------------------------------------------------------------------------
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String[ ] args) {
System.out.println(ages);
}
}
------------------------------------------------------------------
String[] nameArr = new String[ages.size()];
nameArr = ages.keySet().toArray(nameArr);
//////////////////////////////////////////////////////////////////////////////////////
import java.util.HashSet;
public class MyClass {
public static void main(String[ ] args) {
HashSet colors = new HashSet();
colors.add("Red");
colors.add("Blue");
colors.add("Green");
colors.add(3);
colors.remove("Green");
System.out.println(colors);
//System.out.println(colors.get(1));get()-->wont work in hash set
if(colors.contains("Red"))
System.out.println("Present");
if(colors.contains(3))
System.out.println("Present");
System.out.println(colors.size());
colors.clear();
System.out.println(colors);
}
}
HASH SET - add,remove,clear,size,contains - duplicate values are not considered
and are ordered randomly.
//////////////////////////////////////////////////////////////////////////////////////
import java.util.LinkedHashSet;
public class MyClass {
public static void main(String[ ] args) {
LinkedHashSet colors = new LinkedHashSet();
colors.add("Red");
colors.add("Blue");
colors.add("Green");
colors.add(3);
colors.remove("Green");
System.out.println(colors);
//System.out.println(colors.get(1));get()-->wont work in hash set
if(colors.contains("Red"))
System.out.println("Present");
if(colors.contains(3))
System.out.println("Present");
System.out.println(colors.size());
colors.clear();
System.out.println(colors);
}
}
//////////////////////////////////////////////////////////////////////////////////////
import java.util.LinkedList;
import java.util.Collections;
public class MyClass {
public static void main(String[ ] args) {
LinkedList animals = new LinkedList();
animals.add(61);
animals.add(41);
animals.add(33);
animals.add(78);
System.out.println(animals);
Collections.sort(animals);
System.out.println(animals);
Collections.reverse(animals);
System.out.println(animals);
Collections.shuffle(animals);
System.out.println(animals);
System.out.println(Collections.min(animals));
System.out.println(Collections.max(animals));
}
}
----------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Collections;
public class MyClass {
public static void main(String[ ] args) {
ArrayList<String> animals = new ArrayList<String>();
animals.add("tiger");
animals.add("cat");
animals.add("snake");
animals.add("dog");
System.out.println(animals);
Collections.sort(animals);
System.out.println(animals);
Collections.reverse(animals);
System.out.println(animals);
Collections.shuffle(animals);
System.out.println(animals);
System.out.println(Collections.min(animals));
System.out.println(Collections.max(animals));
}
}
SORTING - LINKED LIST / ARRAY LIST - sort,reverse,shuffle,min,max
//////////////////////////////////////////////////////////////////////////////////////
import java.util.Iterator;
import java.util.ArrayList;
public class MyClass {
public static void main(String[ ] args) {
ArrayList<String> animals = new ArrayList<String>();
animals.add("fox");
animals.add("cat");
animals.add("dog");
animals.add("rabbit");
Iterator<String> it1 = animals.iterator();
String value1 = it1.next();
System.out.println(value1);
System.out.println("----------");
for(int i=0; i<animals.size(); i++)
{
Iterator<String> it2 = animals.iterator();
String value2 = it2.next();
System.out.println(value2);
}
System.out.println("----------");
Iterator<String> it3 = animals.iterator();
for(int i=0; i<animals.size(); i++)
{
String value3 = it3.next();
System.out.println(value3);
}
}
}
---------------------------------------------------------------------------
import java.util.Iterator;
import java.util.LinkedList;
public class MyClass {
public static void main(String[ ] args) {
LinkedList<String> animals = new LinkedList<String>();
animals.add("fox");
animals.add("cat");
animals.add("dog");
animals.add("rabbit");
System.out.println(animals);
Iterator<String> it = animals.iterator();
while(it.hasNext()) {
String value = it.next();
System.out.println(value);
}
System.out.println(animals);
}
}
ITERATOR LINED LIST / ARRAY LIST- cycle through a collection to get values -
hasNext(),next(),remove()
//////////////////////////////////////////////////////////////////////////////////////
import java.io.File;
public class Main
{
public static void main(String[] args)
{
File file = new File("C:\\Users\\bala.j.HCLTECH\\Downloads");
if(file.exists())
{
System.out.println("The file "+file.getName()+" is present");
}
else
{
System.out.println("The file is not present");
}
}
}
---------------------------------------------------------------
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
File file = new File("C:\\Users\\BJ2\\NAS old\\NAS_Files\\Bala.txt");
if(file.exists())
{
System.out.println("The file "+file.getName()+" is present");
try {
Scanner sc = new Scanner(file);
while(sc.hasNext())
{
//System.out.println(sc.next());--its right
String x = sc.next();
System.out.println(x);
}
sc.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
System.out.println("The file is not present");
}
}
}
-----------------------------------------------------------------------------
File Handling - To read a file - exists(), getName(), sc.close(),
sc.hasNext(),sc.next(),
//////////////////////////////////////////////////////////////////////////////////////
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Formatter;
//////////////////////////////////////////////////////////////////////////////////////
import java.util.HashMap;
import java.util.Scanner;
import java.util.Map.Entry;
public class Main
{
public static void main(String[] args)
{
HashMap<String,Integer> map = new HashMap<String,Integer>();
Scanner sc = new Scanner(System.in);
while(map.size()<3)
{
String name = sc.next();
int points = sc.nextInt();
map.put(name,points);
}
String[] arr = new String[map.size()];
arr = map.keySet().toArray(arr);
int max = 0;
for(String x : arr)
{
int val = map.get(x);
if(val>max)
{
max = val;
}
}
for(String x : arr)
{
if(map.get(x)!=max)
{
map.remove(x);
}
}
System.out.println(map);
String[] arr1 = new String[map.size()];
arr1 = map.keySet().toArray(arr);
System.out.println(arr1[0]);
}
}
---------------------------------------------------------------------------------
import java.util.HashMap;
import java.util.Scanner;
import java.util.Map.Entry;
public class Main
{
public static void main(String[] args)
{
HashMap<String,Integer> map = new HashMap<String,Integer>();
Scanner sc = new Scanner(System.in);
while(map.size()<3)
{
String name = sc.next();
int points = sc.nextInt();
map.put(name,points);
}
String[] arr = new String[map.size()];
arr = map.keySet().toArray(arr);
int max = 0;
for(String x : arr)
{
int val = map.get(x);
if(val>max)
{
max = val;
}
}
for(Entry<String, Integer> entry: map.entrySet()) // to get a key alone from value use
Entry
{
if(entry.getValue() == max)
{
System.out.println(entry.getKey());
}
}
}
}
-----------------------------------------------------------------------
// to get a key alone from value use Entry
for(Entry<String, Integer> entry: map.entrySet())
{
if(entry.getValue() == max)
{
System.out.println(entry.getKey());
}
}
-------------------------------------------------------------------------
To get the key from value - use Entry - map.entrySet(), entry.getValue(),
entry.getKey()
//////////////////////////////////////////////////////////////////////////////////////