……………….
PRACTICAL-4
AIM: To find average marks of n students and calculate their percentages and assign them
grades.
PROGRAM:
import [Link];
import [Link].*;
import [Link].*;
public class Grade {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n,m1,m2,m3,m4,m5;
int[] a=new int[100];
[Link]("Enter the number of students\n");
n=[Link]();
[Link]("Student no\n");
for(int i=0;i<n;i++){
a[i]=[Link]();
[Link]("\nEnter the marks of student");
[Link]("\nEnter the marks of 1st subject");
m1=[Link]();
[Link]("\nEnter the marks of 2nd subject");
m2=[Link]();
[Link]("\nEnter the marks of 3rd subject");
m3=[Link]();
[Link]("\nEnter the marks of 4th subject");
m4=[Link]();
[Link]("\nEnter the marks of 5th subject");
m5=[Link]();
float avg;
avg=(m1+m2+m3+m4+m5)/5;
[Link]("\nAverage of first 5 marks %.2f\n",avg);
if(avg >= 95) {
[Link]("You got A+ grade\n");
}
else if(avg>=90 && avg<95) {
[Link]("You got A grade\n");
}
GUNEET KAUR
else if(avg>=85 && avg<90){
[Link]("You got B+ grade\n");
}
else if(avg>=80 && avg<85){
[Link]("You got B grade\n");
}
else if(avg>=75 && avg<80){
[Link]("You got C+ grade\n");
}
else if(avg<75){
[Link]("You got C grade\n");
}
}
}
}
GUNEET KAUR
……………….
PRACTICAL-5
AIM: To find biggest of three given numbers.
PROGRAM:
import [Link];
class Largest
{
public static void main(String args[])
{
int x,y,z;
[Link]("enter three integers");
Scanner in =new Scanner([Link]);
x=[Link]();
y=[Link]();
z=[Link]();
if(x>y && x>z)
[Link]("first number is largest");
else if(y>x && y>z)
[Link]("second number is largest");
else if(z>x && z>y)
[Link]("third number is largest");
else
[Link]("the numbers are not distinct");
}
}
GUNEET KAUR
……………….
PRACTICAL-6
AIM: To define a class, describe its constructor, overload the constructor and instantiate its
object.
PROGRAM:
public class A
{
int id, age; String name;
A()
{
id =0;
name = "NULL";
age=0;
}
A(int i,String n,int a)
{
id = i;
name = n;
age=a;
}
A(A a)
{
id=[Link];
name=[Link];
age=[Link];
}
void display(){
[Link](id+" "+name+" "+age);
}
public static void main(String args[]){
A s1 = new A();
A s2 = new A(222,"Aryan",25);
[Link]("Default constructor:"); [Link]();
[Link](" Parameterised constructor:"); [Link]();
A s3=s2;
[Link](" Copy constructor:"); [Link]();
}
}
GUNEET KAUR
……………….
PRACTICAL-7
AIM: To create a Java program to implement stack and queue concept.
PROGRAM:
import [Link];
import [Link].*;
import [Link].*;
class Stack {
static final int MAX = 100;
int top;
int a[] = new int[MAX]; // Maximum size of Stack
boolean isEmpty() {
return (top < 0);
}
Stack() {
top = -1;
}
boolean push(int x){
if (top >= (MAX - 1)) {
[Link]("Stack Overflow");
return false;
}
else {
a[++top] = x;
[Link](x + " pushed into stack");
return true;
}
}
int pop() {
if (top < 0) {
[Link]("Stack Underflow");
return 0;
}
else {
int x = a[top--];
return x;
}
}
GUNEET KAUR
int peek() {
if (top < 0) {
[Link]("Stack Underflow");
return 0;
}
else {
int x = a[top];
return x;
}
}
}
class Queue {
int front, rear, size;
int capacity;
int array[];
public Queue(int capacity) {
[Link] = capacity;
front = [Link] = 0;
rear = capacity - 1;
array = new int[[Link]];
}
boolean isFull(Queue queue) {
return ([Link] == [Link]);
}
boolean isEmpty(Queue queue) {
return ([Link] == 0);
}
void enqueue( int item) {
if (isFull(this))
return;
[Link] = ([Link] + 1)%[Link];
[Link][[Link]] = item;
[Link] = [Link] + 1;
[Link](item+ " enqueued to queue");
}
int dequeue() {
if (isEmpty(this)){
[Link]("Queue is Empty");
return 0;
}
int item = [Link][[Link]];
[Link] = ([Link] + 1)%[Link];
[Link] = [Link] - 1;
GUNEET KAUR
……………….
PRACTICAL-10
AIM: To write a Java program that uses both recursive and non-recursive functions to print the
nth value of Fibonacci series.
PROGRAM:
import [Link].*;
class fibo
{
static int n1=0,n2=1,n3=0;
static void fib(int n)
{
if (n > 0)
{
n3=n1+n2;
n1=n2;
n2=n3;
[Link](" " + n3);
fib(n-1);
}
}
public static void main(String[] args)
{
Scanner s=new Scanner([Link]);
int x,y,n;
int[] a=new int[100];
[Link]("Which method you want to use\[Link]-Recursive\[Link]\[Link]");
x=[Link]();
switch(x){
case 1:[Link]("Enter the value of n:");
n=[Link]();
int t1=0,t2=1;
[Link]("Upto " + n + ": ");
while (t1 < n)
{
[Link](t1 + " ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
}
GUNEET KAUR
break;
case 2:[Link]("Enter the value of n:");
n=[Link]();
[Link](n1+" "+n2);
fib(n-2);
break;
default:break;
}
}
}
GUNEET KAUR