Assignment Pca1
Assignment Pca1
DEMO CLASS:-
package pallindrome;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
n=sc.nextInt();
Pallindrome obj=new Pallindrome(n);
obj.check();
}
}
Output:-
DEMO CLASS:-
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int num;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number ");
num=sc.nextInt();
Perfect obj= new Perfect(num);
obj.check();
}
Output:-
6. Write a program to print all perfect nos. in the range 1-100
Program:-
package pallindrome;
public class Perfect_01 {
public static void main(String []arrgs){
int i,sum=1;
System.out.println("Perfect nos from 1 to 100 are ,");
for(int j=2;j<=100;j++){
sum=1;
for(i=2;i<j;i++){
if(j%i==0)
sum=sum+i;
}
if(j==sum)
System.out.print(j+",");
}
}
}
Output:-
}
DEMO CLASS:-
import java.util.Scanner;
public class Demo {
public static void main(String args[])
{
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number you want to check: ");
n = sc.nextInt();
Magic obj=new Magic(n);
obj.check();
}
}
Output:-
Output:-
9. Write a program to create a class Calculator which performs addition,
subtraction, multiplication, and division using switch case.
Program:-
import java.util.Scanner;
public class Calculator {
public static void main(String []arrgs){
Scanner scanner = new Scanner(System.in);
int num;
System.out.println("1. Addition");
System.out.println("2. Substraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("Choose the operation you want to perform.");
num = scanner.nextInt();
switch(num){
case 1 -> {
int x,y;
System.out.println("Enter number1. ");
x = scanner.nextInt();
System.out.println("Enter number2. ");
y = scanner.nextInt();
int z = x + y;
System.out.println("Sum of two number is "+z);
}
case 2 -> {
int x,y;
System.out.println("Enter number1. ");
x = scanner.nextInt();
System.out.println("Enter number2. ");
y = scanner.nextInt();
int z = x - y;
System.out.println("Sum of two number is "+z);
}
case 3 -> {
int x,y;
System.out.println("Enter number1. ");
x = scanner.nextInt();
System.out.println("Enter number2. ");
y = scanner.nextInt();
int z = x * y;
System.out.println("Sum of two number is "+z);
}
case 4 -> {
int x,y;
System.out.println("Enter number1. ");
x = scanner.nextInt();
System.out.println("Enter number2. ");
y = scanner.nextInt();
int z = x / y;
System.out.println("Sum of two number is "+z);
}
default -> {
System.out.println("you have entered invalid choice");
}
}
}
}
Output:-
Output:-
if (i == array.length)
System.out.println(key + " doesn't exist in array.");
}
}
}
Output:-
Output:-
13. Write a program to show Hierarchical inheritance in java by staff details
of company being given in the problem.
Program:-
Staff class:-
import java.util.Scanner;
Teacher Class:-
import java.util.Scanner;
public class Teacher extends Staff{
//Subject and Department
protected String sub;
protected String dept;
Scanner sc1 = new Scanner(System.in);
public void teacherInput(){
System.out.println("Enter the subject of teacher: ");
sub = sc1.next();
System.out.println("Enter the department of teacher: ");
dept = sc1.next();
}
public void teacherDisplay(){
System.out.println("Subject of teacher: "+sub);
System.out.println("Department of teacher: "+dept);
}
}
Typist class:-
import java.util.Scanner;
public class Typist extends Staff{
//speed
protected int speed;
Scanner sc2 = new Scanner(System.in);
public void typistInput(){
System.out.println("Enter the Speed of typist: ");
speed = sc2.nextInt();
}
public void typistDisplay(){
System.out.println("Number of word "+speed+"per minute.");
}
}
Officer class:-
import java.util.Scanner;
public class Officer extends Staff{
//grade
protected String grade;
Scanner sc3 = new Scanner(System.in);
public void officerInput(){
System.out.println("Enter the Grade of officer: ");
grade = sc3.next();
}
public void officerDisplay(){
System.out.println("Grade of Officer: "+grade);
}
}
Regular class:-
import java.util.Scanner;
public class Regular extends Typist{
//salary
protected double salary;
Scanner sc4 = new Scanner(System.in);
public void regularInput(){
System.out.println("Enter the salary of Regular Typist: ");
salary = sc4.nextDouble();
}
public void regularDisplay(){
System.out.println("Salary of regular Typist: "+salary);
}
}
Casual class:-
import java.util.Scanner;
public class Casual {
//daily_wages
protected double daily_wages;
Scanner sc5 = new Scanner(System.in);
public void casualInput(){
System.out.println("Enter the daily wages of casual typist: ");
daily_wages = sc5.nextDouble();
}
public void casualDisplay(){
System.out.println("Daily Wages of Casual typist: "+daily_wages);
}
}
Demo class:-
import static java.lang.System.exit;
import java.util.Scanner;
public class Demo {
public static void main(String []arrgs){
int ch;
Scanner sc = new Scanner(System.in);
System.out.println(" 1.Teacher\n 2.Typist\n 3.Officer");
System.out.println("Enter your Choice: ");
ch = sc.nextInt();
switch(ch){
case 1 -> {
Teacher obj = new Teacher();
obj.staffInput();
obj.teacherInput();
System.out.println();
obj.staffDislay();
obj.teacherDisplay();
}
case 2 -> {
Typist obj1 = new Typist();
obj1.staffInput();
obj1.typistInput();
System.out.println();
obj1.staffDislay();
obj1.typistDisplay();
System.out.println();
int n;
System.out.println("1.Regular Typist\n2.Casual Typist");
System.out.println("Enter your choice: ");
n = sc.nextInt();
switch(n){
case 1 -> {
Regular obj3 = new Regular();
obj3.regularInput();
System.out.println();
obj3.regularDisplay();
}
case 2 -> {
Casual obj4 = new Casual();
obj4.casualInput();
System.out.println();
obj4.casualDisplay();
}
}
}
case 3 -> {
Officer obj2 = new Officer();
obj2.staffInput();
obj2.officerInput();
System.out.println();
obj2.staffDislay();
obj2.officerDisplay();
}
case 4 -> {
exit(0);
}
default -> {
System.out.println("Invalid Choice");
}
}
}
}
Output:-
DEMO CLASS:-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Demo {
public static void main(String args[]) throws IOException,Exception
{
StackArray s=new StackArray();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,ch;
while(true)
{
System.out.println("Enter 1 to PUSH,\nEnter 2 to POP,\nEnter 3 to
DISPLAY,\ndefault EXIT");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
{
s.push();
break;
}
case 2:
{
s.pop();
break;
}
case 3:
{
s.display();
break;
}
default:
{
System.out.println("EXIT");
System.exit(0);
}
}
}
}
}
Output:-