Pca1 Ritesh Java Lab Assignment
Pca1 Ritesh Java Lab Assignment
Code:
//prime class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ritesh
*/
public class prime {
int n, i, p=1;
prime()
{
//default constructor
}
prime(int m)
{
n=m; //parameterised constructor
}
void checkPrime()
{
for(i=2;i<n;i++){
if(n%i==0) {
p=0;
break;
}
}
if(p==1)
System.out.println(n+ " is Prime Number ");
else
System.out.println(n+ " is not a Prime Number");
}
}
//Driver class
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ritesh
*/
public class Demoprime {
public static void main(String[] args) {
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number");
n=sc.nextInt();
prime obj=new prime(n);
obj.checkPrime();
}
}
OUTPUT:
/**
*
* @author ritesh
*/
public class SumOfDigits1 {
int n=0,num,sum=0;
public SumOfDigits1(int x)
{
num=x;
}
//Driver Class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
public class SumOfDigits1Demo {
public static void main(String[] args) {
int digit;
Scanner sc= new Scanner(System.in);
System.out.println("Enter the Number: ");
digit=sc.nextInt();
SumOfDigits1 obj= new SumOfDigits1(digit);
obj.isSum();
}
}
OUTPUT:
/**
*
* @author ritesh
*/
public class pallin {
int n ,rem, rev=0,temp;
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
System.out.println(" Given Number is =" +temp);
System.out.println("The Reverse of a Number is =" +rev);
if(rev==temp)
System.out.println("\nThe number is pallindrome");
else
System.out.println("\nThe Number is not a pallindrome");
}
}
//Driver Class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ritesh
*/
import java.util.*;
public class Demostration {
public static void main(String[] args) {
int n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
n=sc.nextInt();
pallin obj=new pallin(n);
obj.check();
}
}
OUTPUT:
/**
*
* @author ritesh
*/
public class Perfect {
int i,num,sum;
public Perfect (int x)
{
num=x;
}
void check(){
for(i=1;i<num;i++)
{
if(num%i==0)
{
sum=sum+i;
}
}
if(sum==num)
System.out.println(num+ "is a perfect number");
else
System.out.println(num+ "is not a perfect number");
}
}
//Driver Class
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ritesh
*/
import java.util.*;
public class Deemoo {
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:
/**
*
* @author ritesh
*/
public class perfectno {
public static void main(String[] args)
{
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:
/**
*
* @author ritesh
*/
public class magic {
int n, remainder = 1, number, sum = 0;
/**
*
* @author ritesh
*/
import java.util.*;
public class Demo10 {
public static void main(String args[])
{
int n;
//creating a constructor of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number you want to check: ");
//reading an integer form the user
n = sc.nextInt();
//assigning the entered number in the variable num
magic obj=new magic(n);
obj.check();
}
}
OUTPUT:
/**
*
* @author ritesh
*/
public class Fibonacci {
public static void main(String[] args) {
int count=20, first=0, second=1;
System.out.println("Fibonacci Series upto "+count+ ":");
while(first<=count){
System.out.print(first +",");
int next=first+second;
first=second;
second=next;
}
}
}
OUTPUT:
/**
*
* @author ritesh
*/
import java.util.*;
public class Calculator {
public static void main(String[] args)
{
int m, n, opt, add, sub, mul;
double div;
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number:");
m = sc.nextInt();
System.out.print("Enter second number:");
n = sc.nextInt();
while(true)
{
System.out.println("Enter 1 for addition");
System.out.println("Enter 2 for subtraction");
System.out.println("Enter 3 for multiplication");
System.out.println("Enter 4 for division");
System.out.println("Enter 5 to Exit");
opt = sc.nextInt();
switch(opt)
{
case 1:
add = m + n;
System.out.println("Result:"+add);
break;
case 2:
sub = m - n;
System.out.println("Result:"+sub);
break;
case 3:
mul = m * n;
System.out.println("Result:"+mul);
break;
case 4:
div = (double)m / n;
System.out.println("Result:"+div);
break;
case 5:
System.exit(0);
}
}
}
}
OUTPUT:
/**
*
* @author ritesh
*/
import java.util.*;
public class DecimaltoOctal {
public static void main(String[] args) {
int num,octl=0,i=1,n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Decimal Number :");
num=sc.nextInt();
n=num;
while(n>0)
{
octl+=(n%8)*i;
n/=8;
i*=10;
}
System.out.println("Octal number of "+num+" is "+octl);
}
}
OUTPUT:
Teacher Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
public class Teacher extends Staff{
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
public class Typist extends Staff{
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
public class Officer extends Staff{
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
public class Regular extends Typist{
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
public class Casual extends Typist{
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);
}
}
Driver Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author ritesh
*/
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 : {
System.exit(0);
}
default : {
System.out.println("Invalid Choice");
}
}
}
OUTPUT:
14. Write a program to implement stack using array.
CODE:
//Stack class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
/**
*
* @author ritesh
*/
public class Stackarray {
final int MAX=5;
private final int info[];
private int top;
public Stackarray()
{
info=new int[MAX];
top=-1;
}
public void push()throws Exception
{
if(top==MAX-1)
System.out.println("The Stack OVERFLOW");
else
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int x=Integer.parseInt(br.readLine());
top++;
info[top]=x;
}
}
public void pop()
{
if(top==-1)
System.out.println("The Stack is UNDERFLOW");
else
{
int a;
a=info[top];
top--;
System.out.println("The Poped element is "+a);
}
}
public void display()
{
System.out.println("The elements of the Stack is");
for(int i=0;i<=top;i++)
{
System.out.println(""+info[i]);
}
}
}
//Driver Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
/**
*
* @author ritesh
*/
public class Driver {
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: