Skip to content

Instantly share code, notes, and snippets.

@SuvamPrasd
Created April 8, 2020 14:45
Show Gist options
  • Save SuvamPrasd/0f7ab0111ef36240ea932ddb85aaac80 to your computer and use it in GitHub Desktop.
Save SuvamPrasd/0f7ab0111ef36240ea932ddb85aaac80 to your computer and use it in GitHub Desktop.
Java Practical answers of all the questions but excluded applet questions
package com.suvam;
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Shape{
public Double length;
public Double breadth;
public Shape(Double length, Double breadth){
this.length = length;
this.breadth = breadth;
}
}
class Rect extends Shape{
public Rect(Double length, Double breadth){
super(length,breadth);
}
public void calcRectArea(){
System.out.println("Area of rectangle : " + (length * breadth));
}
}
class Square extends Shape{
public Square(Double length, Double breadth){
super(length,breadth);
}
public void calcSquareArea(){
System.out.println("Area of Square : " + (length * length));
}
}
public class Main {
public static void main(String[] args) {
Double length, breadth;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Enter the sides");
length = Double.parseDouble(br.readLine());
breadth = Double.parseDouble(br.readLine());
Rect r = new Rect(length,breadth);
Square sq = new Square(length,breadth);
//calculate rectangle area
r.calcRectArea();
//calculate square area
sq.calcSquareArea();
}catch (Exception e) {
e.printStackTrace();
}
}
}
package com.suvam;
import java.lang.*;
public class Main {
public static void main(String[] args) {
final String nameArr[][] = {{
"Subhash", "10000"
},{
"Gita", "6000"
},{
"Shiyam", "20000"
}};
double salary;
for(int i = 0 ; i < nameArr.length ; i++){
try{
salary = Double.parseDouble(nameArr[i][1]);
if(salary <= 10000){
System.out.println("employee name : " + nameArr[i][0] + " and salary : " + salary);
}
else throw new Exception("Salary is greater than Rs.10000");
}catch (Exception e){
System.out.println(e);
}
}
}
}
package com.suvam;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.Exception;
import java.lang.Math;
class MyException extends Exception{
public MyException(String s){
super(s);
}
}
public class Main {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Enter a number :");
double num = Double.parseDouble(br.readLine());
if(num >= 0){
System.out.format("Sqaure root of %s is : %.2f",num,Math.sqrt(num));
}else throw new MyException("The number is a negative number");
}catch (MyException e){
System.out.println(e);
}catch (Exception e){
e.printStackTrace();
}
}
}
package com.suvam;
public class Main {
public static void main(String[] args) {
StringBuffer firstString = new StringBuffer();
StringBuffer secondString = new StringBuffer("12");
StringBuffer thirdString = new StringBuffer("Java");
System.out.println("First string ::");
System.out.println(firstString);
System.out.println(firstString.length());
System.out.println(firstString.capacity());
System.out.println("Second string ::");
System.out.println(Integer.parseInt(secondString.toString()));
System.out.println(secondString.length());
System.out.println(secondString.capacity());
System.out.println("Third string ::");
System.out.println(thirdString);
System.out.println(thirdString.length());
System.out.println(thirdString.capacity());
}
}
package com.suvam;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Enter any string::");
String anyString = br.readLine();
System.out.println("String in lowercase: " + anyString.toLowerCase());
System.out.println("String in uppercase: " + anyString.toUpperCase());
System.out.println("Substring from 5 to end: " + anyString.substring(5,anyString.length() - 1));
System.out.println("Substring from 0 to 5: " + anyString.substring(0,5));
System.out.println("Substring from 3 to 7: " + anyString.substring(3,7));
System.out.println("Substring from 5 to 5: " + anyString.substring(5,5));
}catch (Exception e){
e.printStackTrace();
}
}
}
package com.suvam;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.*;
interface Language{
final String[] langArr = {"eng","hindi","spanish","greek"};
}
class Library{
public int bookNo;
Library(int bookNo){
this.bookNo = bookNo;
}
}
class Book extends Library implements Language{
public String author;
public String title;
Book(int bookNo, String author, String title){
super(bookNo);
this.author = author;
this.title = title;
}
}
class Issue extends Book{
Issue(int bookNo, String author, String title){
super(bookNo,author,title);
}
void displayBookInfo(String lang){
System.out.println("Book no: " + bookNo);
System.out.println("Book title: " + title);
System.out.println("Author name: " + author);
if(lang.toLowerCase().startsWith("eng")){
System.out.println("Book language: " + langArr[0]);
}else if(lang.toLowerCase().startsWith("hindi")){
System.out.println("Book language: " + langArr[1]);
}
else{
System.out.println("Book language: unknown");
}
}
}
public class Main {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int bn;
String at, ti, lang;
try{
System.out.println("Enter bookNo, author name, book title, book language sequentially: ");
bn = Integer.parseInt(br.readLine());
at = br.readLine();
ti = br.readLine();
lang = br.readLine();
new Issue(bn,at,ti).displayBookInfo(lang);
}catch (Exception e){
e.printStackTrace();
}
}
}
package com.suvam;
import java.lang.Thread;
//practical question - create three threads with different priorities
class newThread extends Thread{
String call;
newThread(String call){
this.call = call;
}
@Override
public void run() {
System.out.println("call: "+ call + " " + getId());
System.out.println("call: " + call + " " + getName());
super.run();
}
}
public class Main {
public static void main(String[] args) {
newThread n = new newThread("n");
newThread n1 = new newThread("n1");
newThread n2 = new newThread("n2");
n.setPriority(2);
n1.setPriority(8);
n2.setPriority(5);
n.start();
n1.start();
n2.start();
}
}
import java.lang.Thread;
class Natural{
public void printNaturalNumber(String threadName){
try{
Thread.sleep(2000);
System.out.println(threadName + "- Natural number : \n");
for (int i = 1; i < 5; i++){
System.out.println(i);
}
}catch (Exception e){
System.out.println("Thread interrupted!");
}
System.out.println("\n output finished");
}
}
class NewThread extends Thread{
private String threadName;
Natural n;
NewThread(String threadName, Natural n){
this.threadName = threadName;
this.n = n;
}
@Override
public void run() {
synchronized (n){
n.printNaturalNumber(threadName);
}
super.run();
}
}
public class Main {
public static void main(String[] args){
//creating three threads for displaying the natural nos
Natural n = new Natural();
NewThread t1 = new NewThread("first thread", n);
NewThread t2 = new NewThread("second thread", n);
NewThread t3 = new NewThread("third thread", n);
//starting the thread
t1.start();
t2.start();
t3.start();
//wait threads to end
try{
t1.join();
t2.join();
t3.join();
}catch (Exception e){
System.out.println("Interrupted!");
}
}
}
package com.suvam;
import java.lang.Thread;
class DummyThread extends Thread{
@Override
public void run() {
super.run();
}
}
class ThreadDetails{
DummyThread t;
ThreadDetails(DummyThread d){
t = d;
}
public void displayNameAndPriority(){
System.out.println("Thread name: " + t.getName());
System.out.println("Thread priority: " + t.getPriority());
}
public void changeThreadName(){
t.setName("JAVA");
System.out.println("Thread name changed to : " + t.getName());
}
public void displayThreadDetails(){
System.out.println("Details of thread: ");
System.out.println("Thread ID: " + t.getId());
System.out.println("Thread name: " + t.getName());
System.out.println("Thread priority: " + t.getPriority());
System.out.println("Thread State: " + t.getState());
}
}
public class Main {
public static void main(String[] args) {
DummyThread d = new DummyThread();
ThreadDetails td = new ThreadDetails(d);
//display thread name and it's priority
td.displayNameAndPriority();
System.out.println("----------------------");
//change thread name and display the name
td.changeThreadName();
System.out.println("----------------------");
//display the details of the thread
td.displayThreadDetails();
System.out.println("----------------------");
System.out.println("Over");
}
}
package com.suvam;
import java.io.BufferedReader;
import java.io.InputStreamReader;
interface CollegeName{
String collegeName = "LCB college";
}
class Teacher{
String teacherName;
String qualification;
}
class Dept extends Teacher implements CollegeName{
int deptNum;
String deptName;
Dept(int deptNum, String deptName, String teacherName, String qualification){
this.deptNum = deptNum;
this.deptName = deptName;
this.teacherName = teacherName;
this.qualification = qualification;
}
void displayDetails(){
System.out.println("details :");
System.out.println("Teacher name : " + teacherName);
System.out.println("Qualification : " + qualification);
System.out.println("Department name : " + deptName);
System.out.println("Department number: " + deptNum);
System.out.println("College name : " + collegeName);
}
}
public class Main {
public static void main(String[] args) {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
try{
int dnum = Integer.parseInt(bf.readLine());
String dname = bf.readLine();
String tname = bf.readLine();
String tq = bf.readLine();
Dept d = new Dept(dnum, dname, tname, tq);
d.displayDetails();
bf.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
package com.suvam;
class Person{
String name;
int age;
String gender;
public Person(String name, int age, String gender){
this.name = name;
this.age = age;
this.gender = gender;
}
public void showData(){
System.out.println(name + " " + age + " " + gender);
}
}
class Employee extends Person{
String name;
int age;
String gender;
public Employee(String n, int a, String g){
super(n,a,g);
this.name = n;
this.age = a;
this.gender = g;
}
public void showData(){
System.out.println(name + " " + age + " " + gender);
}
}
class Student extends Person{
String name;
int age;
String gender;
public Student(String n, int a, String g){
super(n,a,g);
this.name = n;
this.age = a;
this.gender = g;
}
public void showData(){
System.out.println(name + " " + age + " " + gender);
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student("Suvam",21,"male");
Person p = new Person("Raju",22,"male");
Employee e = new Employee("Kriti",20,"female");
p.showData();
s.showData();
e.showData();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment