Roshan Ass 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Oops

Name:Ankita Tiwari
Regs.no:2021ugcs062

1.

interface Department {
void getDeptName();
void getDeptHead();
}
class Hostel {
protected String hname, hlocaƟon;
int noofroom;
void getHostelName() {
System.out.println("Name Of the Hostel: " + hname);
}
void getHostelLocaƟon() {
System.out.println("Hostel LocaƟon: " + hlocaƟon);
}
void getNoOfRoom() {
System.out.println("Total Room: " + noofroom);
}
}
class Student extends Hostel implements Department {
private String sname, regno, elesub, deptName, deptHead;
private int avgMarks;
void getStudentName() {
System.out.println("Student: " + sname);
}
String getStudentRegNo() {
return regno;
}
void getElecƟveSubject() {
System.out.println("ElecƟve Subject: " + elesub);
}
void getAvgMarks() {
System.out.println("Average Marks: " + avgMarks);
}
public void getDeptName() {
System.out.println("Department Name: " + deptName);
}
public void getDeptHead() {
System.out.println("Department Head: " + deptHead);
}
void addStudent() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Student name: ");
sname = sc.nextLine();
System.out.print("Enter RegistraƟon Number: ");
regno = sc.nextLine();
System.out.print("Enter ElecƟve Subject: ");
elesub = sc.nextLine();
System.out.print("Enter Hostel Name: ");
hname = sc.nextLine();
System.out.print("Enter Hostel LocaƟon: ");
hlocaƟon = sc.nextLine();
System.out.print("Enter Department Name: ");
deptName = sc.nextLine();
System.out.print("Enter Department Head: ");
deptHead = sc.nextLine();
System.out.print("Enter No of Room: ");
noofroom = sc.nextInt();
System.out.print("Enter Average Marks: ");
avgMarks = sc.nextInt();
}
void migrate() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter new Department Name: ");
deptName = sc.nextLine();
System.out.print("Enter new Department Head: ");
deptHead = sc.nextLine();
}
void display() {
getStudentName();
System.out.println("Student RegistraƟon No is: " +
getStudentRegNo());
getElecƟveSubject();
getAvgMarks();
getDeptName();
getDeptHead();
}
}
public class Main {
public sta c void main(String[] args) {
Scanner sc = new Scanner(System.in);
Student[] students = new Student[100];
int studentCount = 0;
while (true) {
System.out.println("\n1. Admit a student");
System.out.println("2. Migrate a student");
System.out.println("3. Display student informaƟon");
System.out.println("4. Exit");
System.out.print("Enter Your Choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
students[studentCount] = new Student();
students[studentCount].addStudent();
studentCount++;
break;
case 2:
System.out.print("Enter RegistraƟon no: ");
String regNo = sc.next();
boolean found = false;
for (int i = 0; i < studentCount; i++) {
if (students[i].getStudentRegNo().equals(regNo)) {
students[i].migrate();
found = true;
break;
}
}
if (!found) {
System.out.println("Student Not Found");
}
break;
case 3:
System.out.print("Enter RegistraƟon no: ");
regNo = sc.next();
found = false;
for (int i = 0; i < studentCount; i++) {
if (students[i].getStudentRegNo().equals(regNo)) {
students[i].display();
found = true;
break;
}
}
if (!found) {
System.out.println("Student Not Found");
}
break;
case 4:
System.out.println("ExiƟng program...");
sc.close();
System.out.println("\nFor VerificaƟon\nName: Roshan
Kumar\nRegno 2021UGCS075\n");
System.exit(0);
break;
default:
System.out.println("Invalid Choice");
}
}

}
}

Code: interface Player {


void play();
}

class Child implements Player {

@Override public void play() {


System.out.println("Child is playing cricket.");
}
}
class Musician implements Player {

@Override public void play() {


System.out.println("Musician play a Guitar");
}
}
class Actor implements Player {

@Override public void play() {


System.out.println("Actor ac ng in drama with role of hero.");
}
}

public class Main {


public sta c void main (String[] args) {
Child c = new Child();
Musician m = new Musician();
Actor a = new Actor();
System.out.println("Meaning of all Player Class:-");
c.play();
m.play();
a.play();

System.out.println("\nFor Verifica on\nName: Roshan kumar


\nReg.No: 2021UGCS075\n");

}
}

3.
interface Exam {
void Percent_call();
}
class Student {
String name;
int roll_no;
int marks1,marks2;
Student(String name,int roll_no,int marks1,int marks2 ) {
this.name=name;
this.roll_no=roll_no;
this.marks1=marks1;
this.marks2=marks2;
}
void show() {
System.out.println("Student Name: "+ name);
System.out.println("Student roll: "+ roll_no);
System.out.println("Marks1: "+ marks1);
System.out.println("Marks2: "+ marks2);
}
}
class Result extends Student implements Exam {
Double per;

Result(String name,int roll_no,int marks1,int marks2) {


super(name, roll_no, marks1, marks2);
}
public void Percent_call() {
per = (double)(marks1+marks2)/2;
}

public void display() {


super.show();
System.out.println("The percentage obtained for "+this.roll_no+"
:"+ this.per+"%");
}
}
public class Main {
public sta c void main(String[] args) {
Result s1=new Result("Ankita Tiwari ",1245,70,80);
Result s2=new Result("Palak Thakur",1248,90,80);
s1.Percent_call();
s1.display();
s2.Percent_call();
s2.display();

System.out.println("\nFor Verifica on\nName: Ankita


Tiwari\nReg.No: 2021UGCS062\n");

}
}
4.
import java.u l.ArrayList;

interface Stack {
void push(int item);
int pop();
boolean isEmpty();
boolean isFull();
}

class FixedSizeStack implements Stack {


private final int maxSize;
private int[] stackArray;
private int top;

public FixedSizeStack(int size) {


maxSize = size;
stackArray = new int[maxSize];
top = -1;
}

public void push(int item) {


if (isFull()) {
System.out.println("Stack Overflow");
} else {
stackArray[++top] = item;
}
}

public int pop() {


if (isEmpty()) {
System.out.println("Stack Underflow");
return -1;
} else {
return stackArray[top--];
}
}

public boolean isEmpty() {


return top == -1;
}

public boolean isFull() {


return top == maxSize - 1;
}
}

class DynamicArrayStack implements Stack {


private ArrayList<Integer> stackList;

public DynamicArrayStack() {
stackList = new ArrayList<>();
}

public void push(int item) {


stackList.add(item);
}

public int pop() {


if (isEmpty()) {
System.out.println("Stack Underflow");
return -1;
} else {
int item = stackList.get(stackList.size() - 1);
stackList.remove(stackList.size() - 1);
return item;
}
}

public boolean isEmpty() {


return stackList.isEmpty();
}

public boolean isFull() {


// Dynamic array never gets full
return false;
}
}

public class Main {


public sta c void main(String[] args) {
FixedSizeStack fixedStack = new FixedSizeStack(5);
DynamicArrayStack dynamicStack = new DynamicArrayStack();

fixedStack.push(1);
fixedStack.push(2);
fixedStack.push(3);

dynamicStack.push(10);
dynamicStack.push(20);
dynamicStack.push(30);

System.out.println("FixedSizeStack Pop: " + fixedStack.pop());


System.out.println("DynamicArrayStack Pop: " +
dynamicStack.pop());
System.out.println("\nFor Verifica on\nName: Roshan
kumar\nReg.No: 2021UGCS072\n");
}
}

5.
package MCA;

public class Student {


private String name;
private int rollNumber;
private int marks1;
private int marks2;
private int marks3;
public Student(String name, int rollNumber, int marks1, int marks2,
int marks3) {
this.name = name;
this.rollNumber = rollNumber;
this.marks1 = marks1;
this.marks2 = marks2;
this.marks3 = marks3;
}

public void display() {


System.out.println("Student Name: " + name);
System.out.println("Roll Number: " + rollNumber);
System.out.println("Marks in Subject 1: " + marks1);
System.out.println("Marks in Subject 2: " + marks2);
System.out.println("Marks in Subject 3: " + marks3);
}

public int calculateTotalMarks() {


return marks1 + marks2 + marks3;
}

public double calculatePercentage() {


int totalMarks = calculateTotalMarks();
return (totalMarks / 3.0);
}
}

import MCA.Student;

public class Main {


public sta c void main(String[] args) {
// Crea ng a student object with details
Student student = new Student("Nayan Sagar", 101, 85, 90, 78);
Student student1 = new Student("Babu Lal", 102, 75, 88, 92);

// Displaying student details


System.out.println("Student Details:");
student.display();
student1.display();

// Calcula ng total marks and percentage


int totalMarks = student.calculateTotalMarks();
double percentage = student.calculatePercentage();

int totalMarks1 = student1.calculateTotalMarks();


double percentage1 = student1.calculatePercentage();

System.out.println("Total Marks for Student 1: " + totalMarks);


System.out.println("Percentage for Student 1: " + percentage +
"%");

System.out.println("Total Marks for Student 2: " + totalMarks1);


System.out.println("Percentage for Student 2: " + percentage1 +
"%");

System.out.println("\nFor Verifica on\nName: Rohit


Dayal\nReg.No: 2021UGCS082\n");
}
}

6.a
// In Server.java
package mygame.server;
public class Server {
// class implementa on
}

// In U li es.java
package mygame.shared;

public class U li es {
// class implementa on
}

// In Client.java
package mygame.client;

public class Client {


// class implementa on
}
6.b
mygame/
client/
Client.java
server/
Server.java
shared/
U li es.java

7.
package pk1;

public class Pub {


public void display() {
System.out.println("Public Access Class from pk1");
}
}

package pk1;

class Prot {
protected void display() {
System.out.println("Protected Access Class from pk1");
}
}

package pk1;
class Def {
void display() {
System.out.println("Default (Package-Private) Access Class from
pk1");
}
}

package pk2;

import pk1.Prot;

public class ProtInh extends Prot {


public void display() {
System.out.println("Inherited Protected Access Class from pk2");
}
}

package pk2;

import pk1.Def;

public class DefInh extends Def {


// DefInh can access Def's methods because they are in the same
package.
}

import pk1.Pub;
import pk1.Prot;
import pk1.Def;
import pk2.ProtInh;
import pk2.DefInh;

public class Main {


public sta c void main(String[] args) {
Pub p = new Pub();
p.display(); // Accessible from anywhere

Prot pr = new Prot();


// pr.display(); // Error: Not accessible outside package

Def d = new Def();


// d.display(); // Error: Not accessible outside package

ProtInh prInh = new ProtInh();


prInh.display(); // Accessible through inheritance

DefInh dInh = new DefInh();


dInh.display(); // Accessible through inheritance
}
}

You might also like