III Dcme Java Lab
III Dcme Java Lab
/**
* This program demonstrates keyboard input.
*/
public class RectangleArea
{
public static void main(String[] args)
{
int length; // To hold rectangle's length.
int width; // To hold rectangle's width.
int area; // To hold rectangle's area
// Calculate area.
area = length * width;
// Display area.
System.out.println("The area of rectangle is " + area);
}
}
ii)
// Main class
// DataOutputStreamDemo
class GFG {
// Main driver method
public static void main(String args[]) throws IOException {
// Try block to check for exceptions
// Writing the data using DataOutputStream
try ( DataOutputStream dout =
new DataOutputStream(new
FileOutputStream("file.dat")) ) {
dout.writeDouble(1.1);
dout.writeInt(55);
dout.writeBoolean(true);
dout.writeChar('4');
}
boolean c = din.readBoolean();
char d = din.readChar();
// Main class
class DataInputStreamDemo {
dout.writeDouble(1.1);
dout.writeInt(55);
dout.writeBoolean(true);
dout.writeChar('4');
}
iii)
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
ii)reverse a string
class Lamp {
class Main {
public static void main(String[] args) {
i)constructor
class MyClass
{
MyClass()
{
System.out.println("No-argument constructor called");
}
ii)constructor overloding
Source Code
class Box
{
double width, height, depth;
Box()
{
width = 1;
height = 1;
depth = 1;
}
void myMethod()
{
System.out.println("Width : " + width);
System.out.println("Height : " + height);
System.out.println("Depth : " + depth);
}
}
}
7.
i)
class Example2{
public static void main(String[] args){
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int sum = a + b;
System.out.println("The sum is" +sum);
}
}
ii)
class Adder{
static int add(int a,int b){
return a+b;
}
static int add(int a,int b,int c){
return a+b+c;
}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}
}
9.Inheritnce
i) single
Source Code
public class SingleInheritance // Main class
{
public static void main(String[] args)
{
Dog dog = new Dog("Buddy"); // Create an instance of Dog
ii)multilevel
Source Code
public class MultilevelInheritance // Main class
{
public static void main(String[] args)
{
Puppy puppy = new Puppy("Buddy"); // Create an instance of Puppy
iii)Hierarchical
10)Method overriding
11)Packages
i)creation of package
package library;
import library.domain.Book;
package myPackage;
import java.util.*;
int i = read.nextInt();
System.out.println("You have entered a number " + i);
12)Interfces
import java.io.*;
// A simple interface
interface In1 {
// Driver Code
public static void main(String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(t.a);
}
}
13)Collections
import java.util.Scanner;
public class Student_Marks
{
public static void main(String[] args)
{
int n, total = 0, percentage;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of subject:");
n = s.nextInt();
int marks[] = new int[n];
System.out.println("Enter marks out of 100:");
for(int i = 0; i < n; i++)
{
marks[i] = s.nextInt();
total = total + marks[i];
}
percentage = total / n;
System.out.println("Sum:"+total);
System.out.println("Percentage:"+percentage);
}
}
ii)Linked List
import java.util.LinkedList;
class Main {
public static void main(String[] args) {
LinkedList<String> languages = new LinkedList<>();
import java.util.HashMap;
public class Main {
static final int SIZE = 10; // Define the size of the hash table
static class DataItem {
int key;
}
static HashMap<Integer, DataItem> hashMap = new HashMap<>(); // Define the hash
table as a HashMap
Collections.sort(sortedKeys);
// Driver Code
public static void main(String args[])
{
// putting values in the Map
map.put("Jayant", 80);
map.put("Abhishek", 90);
map.put("Anushka", 80);
map.put("Amit", 75);
map.put("Danish", 40);
i)try, catch,finlly
finlly
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
}
System.out.println("normal flow..");
}
}
15)Exercises on multithreading
//single thread
import java.io.*;
class GFG extends Thread {
public void run()
{
System.out.print("Welcome to GeeksforGeeks.");
}
public static void main(String[] args)
{
GFG g = new GFG(); // creating thread
g.start(); // starting thread
}
}
// object creation
Main t2 = new Main();
// object creation
Main t3 = new Main();
// Main class
class ThreadDemo extends Thread {
// Method 1
// run() method for the thread that is called
// as soon as start() is invoked for thread in main()
public void run()
{
// Print statement
System.out.println("Inside run method");
}
// Thread 1
// Display the priority of above thread
// using getPriority() method
System.out.println("t1 thread priority : "
+ t1.getPriority());
// Thread 1
// Display the priority of above thread
System.out.println("t2 thread priority : "
+ t2.getPriority());
// Thread 3
System.out.println("t3 thread priority : "
+ t3.getPriority());
// 5
System.out.println("t2 thread priority : "
+ t2.getPriority());
// 8
System.out.println("t3 thread priority : "
+ t3.getPriority());
// Main thread
System.out.println(
"Main thread priority : "
+ Thread.currentThread().getPriority());
System.out.println(
"Main thread priority : "
+ Thread.currentThread().getPriority());
}
}
import java.util.Scanner;
// t1 finishes before t2
t1.join();
t2.join();
}
// Sleeps for some time and waits for a key press. After key
// is pressed, it notifies produce().
public void consume()throws InterruptedException
{
// this makes the produce thread to run first.
Thread.sleep(1000);
Scanner s = new Scanner(System.in);
// synchronized block ensures only one thread
// running at a time.
synchronized(this)
{
System.out.println("Waiting for return key.");
s.nextLine();
System.out.println("Return key pressed");
// Sleep
Thread.sleep(2000);
}
}
}
}