Java 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

……………….

PRACTICAL-4
AIM: To find average marks of n students and calculate their percentages and assign them
grades.

PROGRAM:
import java.util.Scanner;
import java.io.*;
import java.util.*;
public class Grade {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n,m1,m2,m3,m4,m5;
int[] a=new int[100];
System.out.print("Enter the number of students\n");
n=sc.nextInt();
System.out.print("Student no\n");
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
System.out.print("\nEnter the marks of student");
System.out.print("\nEnter the marks of 1st subject");
m1=sc.nextInt();
System.out.print("\nEnter the marks of 2nd subject");
m2=sc.nextInt();
System.out.print("\nEnter the marks of 3rd subject");
m3=sc.nextInt();
System.out.print("\nEnter the marks of 4th subject");
m4=sc.nextInt();
System.out.print("\nEnter the marks of 5th subject");
m5=sc.nextInt();
float avg;
avg=(m1+m2+m3+m4+m5)/5;
System.out.format("\nAverage of first 5 marks %.2f\n",avg);
if(avg >= 95) {
System.out.format("You got A+ grade\n");
}
else if(avg>=90 && avg<95) {
System.out.format("You got A grade\n");
}

GUNEET KAUR
else if(avg>=85 && avg<90){

System.out.format("You got B+ grade\n");


}
else if(avg>=80 && avg<85){
System.out.format("You got B grade\n");
}
else if(avg>=75 && avg<80){
System.out.format("You got C+ grade\n");
}
else if(avg<75){
System.out.format("You got C grade\n");
}
}
}
}

GUNEET KAUR
……………….
PRACTICAL-5
AIM: To find biggest of three given numbers.

PROGRAM:
import java.util.Scanner;
class Largest
{
public static void main(String args[])
{
int x,y,z;
System.out.println("enter three integers");
Scanner in =new Scanner(System.in);
x=in.nextInt();
y=in.nextInt();
z=in.nextInt();
if(x>y && x>z)
System.out.println("first number is largest");
else if(y>x && y>z)
System.out.println("second number is largest");
else if(z>x && z>y)
System.out.println("third number is largest");
else
System.out.println("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=a.id;
name=a.name;
age=a.age;
}
void display(){
System.out.println(id+" "+name+" "+age);
}
public static void main(String args[]){
A s1 = new A();
A s2 = new A(222,"Aryan",25);
System.out.println("Default constructor:"); s1.display();
System.out.println(" Parameterised constructor:"); s2.display();
A s3=s2;
System.out.println(" Copy constructor:"); s3.display();
}
}

GUNEET KAUR
……………….
PRACTICAL-7
AIM: To create a Java program to implement stack and queue concept.

PROGRAM:
import java.util.Scanner;
import java.io.*;
import java.util.*;
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)) {
System.out.println("Stack Overflow");
return false;
}
else {
a[++top] = x;
System.out.println(x + " pushed into stack");
return true;
}
}
int pop() {
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top--];
return x;
}
}

GUNEET KAUR
int peek() {
if (top < 0) {
System.out.println("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) {
this.capacity = capacity;
front = this.size = 0;
rear = capacity - 1;
array = new int[this.capacity];
}
boolean isFull(Queue queue) {
return (queue.size == queue.capacity);
}
boolean isEmpty(Queue queue) {
return (queue.size == 0);
}
void enqueue( int item) {
if (isFull(this))
return;
this.rear = (this.rear + 1)%this.capacity;
this.array[this.rear] = item;
this.size = this.size + 1;
System.out.println(item+ " enqueued to queue");
}
int dequeue() {
if (isEmpty(this)){
System.out.println("Queue is Empty");
return 0;
}
int item = this.array[this.front];
this.front = (this.front + 1)%this.capacity;
this.size = this.size - 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 java.util.*;
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;
System.out.print(" " + n3);
fib(n-1);
}
}
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int x,y,n;
int[] a=new int[100];
System.out.println("Which method you want to use\n1.Non-Recursive\n2.Recursive\n3.Exit");
x=s.nextInt();
switch(x){
case 1:System.out.println("Enter the value of n:");
n=s.nextInt();
int t1=0,t2=1;
System.out.print("Upto " + n + ": ");
while (t1 < n)
{
System.out.print(t1 + " ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
}

GUNEET KAUR
break;
case 2:System.out.println("Enter the value of n:");
n=s.nextInt();
System.out.print(n1+" "+n2);
fib(n-2);
break;
default:break;
}
}
}

GUNEET KAUR

You might also like