0% found this document useful (0 votes)
45 views9 pages

Experiment 2 (A) Aim: Theory:: To Calculate Average Marks of Student Using Classes and Objects in Java

The document describes two experiments involving classes and objects in Java. Experiment 2(A) calculates the average marks of students using an array and class. It takes student marks as input, calculates the average, and outputs the grade. Experiment 2(B) implements stack and queue data structures using classes. It demonstrates pushing and popping elements from both stack and queue and outputs the results.

Uploaded by

Shubham Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views9 pages

Experiment 2 (A) Aim: Theory:: To Calculate Average Marks of Student Using Classes and Objects in Java

The document describes two experiments involving classes and objects in Java. Experiment 2(A) calculates the average marks of students using an array and class. It takes student marks as input, calculates the average, and outputs the grade. Experiment 2(B) implements stack and queue data structures using classes. It demonstrates pushing and popping elements from both stack and queue and outputs the results.

Uploaded by

Shubham Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 9

Experiment 2(A)

AIM: To calculate average marks of student using Classes and Objects in Java.

THEORY:

Java provides a data structure, the array, which stores a fixed-size sequential collection
of elements of the same type.

Syntax: dataType[] arrayRefVar = new dataType[arraySize];

For example: double[] myList = new double[10];

SOURCE CODE:
import java.util.Scanner; class Grade {

double avg = 0; String grade = ""; Grade(int a[], int n) {

for (int i = 0; i < a.length; i++) { avg = avg + a[i];

avg = avg / n;

if (avg >= 90 && avg <= 100) { grade = "Excellent";

} else if (avg >= 80 && avg < 90) { grade = "Very Good";

} else if (avg >= 70 && avg < 80) { grade = "Good";

} else if (avg >= 60 && avg < 70) { grade = "Average";

} else if (avg <= 60 && avg > 0) { grade = "Poor";

public class exp2A {

public static void main(String args[]) { System.out.println("Enter number of subjects"); Scanner


sc = new Scanner(System.in);
int n = sc.nextInt(); int[] a = new int[n];

System.out.println("Enter marks"); for (i = 0; i < n; i++) {

a[i] = sc.nextInt();

Grade c = new Grade(a, n);


System.out.print("Grade: " + c.grade);

Output:

Enter number of subjects 6

Enter marks 80 90 100 70 60 80

Grade: Very Good


Experiment 2(B)

AIM: To implement Stack and Queue using Classes and Objects in Java.

THEORY:
A stack is an ordered list in which all insertions and deletions are made at one end, called the top. A
queue is an ordered list in which all insertions take place at one end, the rear, while all deletions take
place at the other end, the front. The restrictions on a stack imply that if the elements are added to
the stack, n that order, then the first element to be popped must the last element to be inserted into
the stack. For this reason stacks are sometimes referred to as Last In First Out (LIFO) lists. The
restrictions on queue imply that the first element which is inserted into the queue will be the first one
to be removed. Thus queues are known as First In First Out (FIFO)

SOURCE CODE:

// Stack

import java.util.*; class Stack {

int[] a = new int[6]; int top = -1;

void push(int x) { if (top == 5) {

System.out.println("Stack Overflow"); } else {

a[++top] = x;

void pop() {

if (top == -1) { System.out.println("Stack Downflow"); } else {

System.out.println(a[top--] + " is Popped");

void display() {

for (int i = 0; i < top + 1; i++) { System.out.print(a[i]+" ");


}

System.out.print("\n");
}

public class exp2B {

public static void main(String args[])


{ Stack s = new Stack();

s.push(10);

s.push(20);

s.push(30);

s.push(40);

s.push(50);

s.push(60);

System.out.println("Stack After
Pushing:"); s.display();

s.pop();

s.pop();

System.out.println("Stack After Pop:");


s.display();

Output:
Stack After Pushing: 10 20 30 40 50 60 60
is Popped

50 is Popped Stack After Pop: 10 20 30 40

//Queue

import java.util.*; public class Queue


{ int[] a = new int[6]; int rear = -1, front =
0; void push(int x) {
a[++rear] = x;

void pop() {

if (front == 0 && rear == 0) { front = -1;

rear = -1; } else { front++;

void display() {

for (int i = front; i <= rear; i++)


{ System.out.print(a[i]+" ");

System.out.print("\n");

public static void main(String args[])


{ Queue q1 = new Queue(); q1.push(10);

q1.push(20);

q1.push(30);

q1.push(40);

q1.push(50);

q1.push(60);

System.out.println("Queue After
Pushing:"); q1.display();

q1.pop();

System.out.println("Queue After
Popping:"); q1.display();

}
}

Output:
Queue After Pushing: 10 20 30 40 50 60
Queue After Popping: 20 30 40 50 60

You might also like