0% found this document useful (0 votes)
7 views

JAVA Class Notes

The document discusses Java concepts like constructors, getter and setter methods, inheritance, polymorphism, abstract classes, exceptions, interfaces, abstract methods, abstract classes, and generics through examples. It also provides examples of using common Java collection classes like ArrayList, LinkedList, HashMap.

Uploaded by

redawej816
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

JAVA Class Notes

The document discusses Java concepts like constructors, getter and setter methods, inheritance, polymorphism, abstract classes, exceptions, interfaces, abstract methods, abstract classes, and generics through examples. It also provides examples of using common Java collection classes like ArrayList, LinkedList, HashMap.

Uploaded by

redawej816
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

JAVA

constructors, getter setter , public


//////////////////////////////////////////////////////////////////////////////////////
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
String movie = read.nextLine();
int row = read.nextInt();
int seat = read.nextInt();
Ticket ticket = new Ticket(movie, row, seat);
System.out.println("Movie: " + ticket.getMovie());
System.out.println("Row: " + ticket.getRow());
System.out.println("Seat: " + ticket.getSeat());
}
}
class Ticket {
private String movie;
private int row;
private int seat;
public Ticket(String movie, int row, int seat) {
this.setMovie(movie);
this.setRow(row);
this.setSeat(seat);
}
public String getMovie() {
return movie;
}
public int getRow() {
return row;
}
public int getSeat() {
return seat;
}
public void setMovie(String movie) {
this.movie = movie;
}
public void setRow(int row) {
this.row = row;
}
public void setSeat(int seat) {
this.seat = seat;
}
}
--------------------------------------------------
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
String movie = read.nextLine();
int row = read.nextInt();
int seat = read.nextInt();
Ticket ticket = new Ticket(movie, row, seat);
System.out.println("Movie: " + ticket.getMovie());
System.out.println("Row: " + ticket.getRow());
System.out.println("Seat: " + ticket.getSeat());
}
}
class Ticket {
private String movie;
private int row;
private int seat;
public Ticket(String movie, int row, int seat) {
this.movie = movie;
this.row = row;
this.seat = seat;
}
public String getMovie() {
return movie;
}
public int getRow() {
return row;
}
public int getSeat() {
return seat;
}
}
----------------------------------------------------------
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
String movie = read.nextLine();
int row = read.nextInt();
int seat = read.nextInt();
Ticket ticket = new Ticket(movie, row, seat);
System.out.println("Movie: " + ticket.movie);
System.out.println("Row: " + ticket.row);
System.out.println("Seat: " + ticket.seat);
}
}
class Ticket {
public String movie;
public int row;
public int seat;
public Ticket(String movie, int row, int seat) {
this.movie = movie;
this.row = row;
this.seat = seat;
}
}

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

public int getAge() {


return age;
}

public void setAge(int a) {


this.age = a;
}
}

//////////////////////////////////////////////////////////////////////////////////////
//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();
}
}

Abstract class - if class in abstract, itshould have a abstract method...if a method is


abstract, the class must also be absract.
In Java, abstraction is achieved using abstract classes and interfaces.
An abstract class is defined using the abstract keyword.
- If a class is declared abstract it cannot be instantiated (you cannot create objects of
that type).
- To use an abstract class, you have to inherit it from another class.
- Any class that contains an abstract method should be defined as abstract.

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

ARRAY LIST - add,remove,clear,get,size,contains

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

LINKED LIST - add,remove,clear,get,size,contains

//////////////////////////////////////////////////////////////////////////////////////
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");

}
}

HASHMAP / HASH MAP - put,remove,get,size,containsKey,containsValue,keySet()

//////////////////////////////////////////////////////////////////////////////////////
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) {

Scanner scanner = new Scanner(System.in);

HashMap<String, Integer> ages = new HashMap<String, Integer>();


ages.put("David", 22);
ages.put("Tom", 23);
ages.put("Robert", 32);
ages.put("Alice", 21);
ages.put("Sophie", 19);
ages.put("Maria", 24);
ages.put("John", 28);
int ageLimit = scanner.nextInt();

String[] nameArr = new String[ages.size()];


nameArr = ages.keySet().toArray(nameArr);
System.out.println(ages.keySet());
for (String emp : nameArr){
if(ages.get(emp)<ageLimit)
ages.remove(emp);

System.out.println(ages);
}
}
------------------------------------------------------------------
String[] nameArr = new String[ages.size()];
nameArr = ages.keySet().toArray(nameArr);

to convert a hasmap key set to array elements

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

LINKED HASH SET- add,remove,clear,size,contains - duplicate values are not


considered and are ordered which element is added first.

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

Iterator<String> it1 = animals.iterator();


String value1 = it1.next();
String value2 = it1.next();
String value3 = it1.next();

it1.remove(); // removes the current last iteration value3 - dog


System.out.println(value1);
System.out.println(value2);
System.out.println(value3);

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;

public class Main


{
public static void main(String[] args) throws FileNotFoundException
{
Formatter f = new Formatter("C:\\Users\\BJ2\\NAS old\\NAS_Files\\Bala1.txt");
f.format("%s %s %s \n","J","Bala","Sundar");
f.format("%s,%s,%s",1,2,3);
f.close();

File file = new File("C:\\Users\\BJ2\\NAS old\\NAS_Files\\Bala1.txt");


if(file.exists())
{
System.out.println("The file "+file.getName()+" exists");
Scanner sc= new Scanner(file);
while(sc.hasNext())
{
System.out.println(sc.next());
}
sc.close();
}
else
{
System.out.println("The file is not present");
}
}
}
-----------------------------------------------------------------------------
import java.util.Scanner;
import java.util.Formatter;
import java.io.File;
import java.io.FileNotFoundException;
public class Main
{
public static void main(String[] args) throws
FileNotFoundException
{
String fileName = "tasks.txt"; //creating & writing a file
Formatter f = new Formatter(fileName);
Scanner sc = new Scanner(System.in);
for(int i=0;i<3;i++)
{
String input = sc.next();
f.format("%s \n",input);
}
f.close();
readFile(fileName);
}

public static void readFile(String name) throws


FileNotFoundException
{
File file = new File(name);
if(file.exists())
{
Scanner sc1 = new Scanner(file);
while(sc1.hasNext())
{
System.out.println(sc1.next());
}
sc1.close();
}
}
}
-----------------------------------------------------------------------------
import java.util.Scanner;
import java.util.Formatter;
import java.io.File;
import java.io.FileNotFoundException;
public class Main
{
public static void main(String[] args) throws
FileNotFoundException
{
String fileName = "tasks.txt"; //creating & writing a file
Formatter f = new Formatter(fileName);
Scanner sc = new Scanner(System.in);
for(int i=0;i<3;i++)
{
String input = sc.next();
f.format("%s \n",input);
}
f.close();
Demo x = new Demo();
x.readFile(fileName);
}
}
public class Demo
{
public void readFile(String name) throws FileNotFoundException
{
File file = new File(name);
if(file.exists())
{
Scanner sc1 = new Scanner(file);
while(sc1.hasNext())
{
System.out.println(sc1.next());
}
sc1.close();
}
}
}
-----------------------------------------------------------------------------
File Handling - To write a file - format("%s", Bala), close()

//////////////////////////////////////////////////////////////////////////////////////
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()

//////////////////////////////////////////////////////////////////////////////////////

You might also like