Swapnanil Dutta Roll 12 Day 8 PCC-CS593
Swapnanil Dutta Roll 12 Day 8 PCC-CS593
Swapnanil Dutta Roll 12 Day 8 PCC-CS593
a) Write a JAVA Program to make a Student class with proper attributes like roll
no, name, stream, and college. From main create such two students and show their information.
Code:
import java.util.Scanner;
public class Student{
String Name,Stream,College,name,stream,college;
int rollNo,RollNo;
void display(){
System.out.println("\nName: "+Name+" \nRoll No: "+rollNo+"
\nStream: "+Stream+" \nCollege: "+College);
}
public static void main(String[]args){
System.out.println("-------------------------Input Student
Details--------------------------");
Scanner st=new Scanner(System.in);
System.out.print("\nEnter The Name of 1st Student: ");
String Name = new Scanner(System.in).nextLine();
System.out.print("Enter Roll No: ");
nt rollNo = st.nextInt();
i
System.out.print("Enter Stream: ");
String Stream = new Scanner(System.in).nextLine();
System.out.print("Enter College: ");
String College = new Scanner(System.in).nextLine();
Student s=new Student(Name,rollNo,Stream,College);
System.out.print("\nEnter The Name of 2nd Student: ");
String name = new Scanner(System.in).nextLine();
System.out.print("Enter Roll No: ");
int RollNo = st.nextInt();
System.out.print("Enter Stream: ");
String stream = new Scanner(System.in).nextLine();
System.out.print("Enter College: ");
String college = new Scanner(System.in).nextLine();
Student t=new Student(name,RollNo,stream,college);
System.out.print("---------------------------Student
Details---------------------------------");
s.display();
t.display();
}
}
Output:
Question: 8.b)Write a JAVA Program to consider the Student class in the previous Program.
Assume that a student studies 6 subjects. Each subject has a title, internal marks and theory
marks. Write a Program to define Student class including the subjects as array. From main
create such two students and show their information including subjects’ name and grand total
marks.
Code:
class Subjects{
String code, title;
int part1, part2, marks = 0;
public Subjects(String code, String title, int part1, int part2){
this.code=code;
this.title=title;
this.part1=part1;
this.part2=part2;
}
}
class Student{
String name, stream;
int roll, total = 0, count = 0;
public Student(int roll, String name, String stream, Subjects[] sub){
this.name=name;
this.roll=roll;
this.stream=stream;
for(Subjects s : sub){
s.marks = s.part1 + s.part2;
System.out.println("Code: " + s.code + " Title: " + s.title +
" Marks: " + s.marks);
total += s.marks;
count++;
}
}
public String toString(){
return "\nName: " + name + "\nRoll: " + roll + "\nStream: " +
stream + "\nTotal " + total;
}
}
public class Student8b{
public static void m
ain(String[] args){
Subjects sub[] = new Subjects[3];
System.out.println("----------------Student
Details---------------");
System.out.println("\n-----------------Student
1-------------------");
sub[0] = new Subjects(" IT501", "Object Oriented Programming", 32,
54);
sub[1] =
n ew S ubjects(" IT502", " Computer Organisation", 36, 54);
sub[2] = n ew S ubjects(" IT503", " Data Structures", 28, 48);
Student s1 = new Student(12, "Swapnanil Dutta", "IT", sub);
System.out.println("\n-----------------Student
2-------------------");
sub[0] = new Subjects(" CSE501", "Object Oriented Programming",
40, 49);
sub[1] = new Subjects(" CSE502", "Computer Organisation", 34, 53);
sub[2] = new S
ubjects(" CSE503", "Data Structures", 29, 58);
Student s2 = n ew Student(114, "Debdut Goswami", "CSE", sub);
System.out.println(s1);
System.out.println(s2);
}
}
Output:
Question: 8.c)Write a JAVA Program to consider the Student class in the first Program 8a.
Assume that students study varying number of subjects. Each subject has a title, internal marks
and theory marks. Write a Program to define Student class including the subjects as vararg
argument of constructor. From main create such two students and show their information
including subjects’ name and grand total marks.
Code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class Subject{
String title;
double internal, theory, totalMarks;
Subject(String title, double internal, double theory, double
totalMarks) {
this.title = title;
this.internal = internal;
this.theory = theory;
this.totalMarks= internal + theory;
}
public String toString() {
return ("Subject Title = " + title + "; Internal marks = " +
internal + "/30" + " Theory = " + theory
+"/70" + "\n Total Marks =" +this.totalMarks+"\n");
}
}
public class Student8c{
String name, stream, college, semester;
int rollNo;
double totalMarks;
tudent8c(String name, String stream, String college, int rollNo,
S
String semester,double Total, Subject...sub) {
this.name = name;
this.stream = stream;
this.college = college;
this.rollNo = rollNo;
this.semester= semester;
this.totalMarks = Total;
for (Subject s : sub){
s.totalMarks = s.internal + s.theory;
}
}
public String toString() {
return "\nStudent Name = "+this.name + "\nRoll no = " +
this.rollNo + "
\nStream = " + this.stream + "\nSemester = " +
this.semester + "\nCollege = "
+ this.college + "\nTotal Marks = "+this.totalMarks;
}
public static void main(String[] args) throws IOException {
BufferedReader buf = new BufferedReader(new
InputStreamReader(System.in));
double Total=0.0;
String name, stream, college, semester;
int rollNo, n;
System.out.println("\n----------Student 1---------");
System.out.print("Name: ");
name = buf.readLine();
System.out.print("Stream: ");
stream = buf.readLine();
System.out.print("Semester: ");
semester = buf.readLine();
System.out.print("College: ");
college = buf.readLine();
System.out.print("Roll no: ");
rollNo = Integer.parseInt(buf.readLine());
System.out.print("Number of Subjects: ");
n = Integer.parseInt(buf.readLine());
Subject[] subs = new Subject[n];
for (int i = 0; i < n; i++) {
System.out.println("Subject " + (i + 1));
System.out.print("Title: ");
String title = buf.readLine();
System.out.print("Internal Marks (Out of 30): ");
ouble internal = Double.parseDouble(buf.readLine());
d
System.out.print("Theory Marks (Out of 70): ");
ouble theory =
d Double.parseDouble(buf.readLine());
subs[i] = new S ubject(title, internal, theory, internal +
theory);
Total+=internal + theory;
}
Student8c Student1 = new Student8c(name, stream, college, rollNo,
semester, Total );
Total=0.0;
System.out.println("\n----------Student 2---------");
System.out.print("Name: ");
name = buf.readLine();
System.out.print("Stream: ");
stream = buf.readLine();
System.out.print("Semester: ");
semester = buf.readLine();
System.out.print("College: ");
college = buf.readLine();
System.out.print("Roll no: ");
rollNo = Integer.parseInt(buf.readLine());
System.out.print("Number of Subjects: ");
n = Integer.parseInt(buf.readLine());
subs = new S
ubject[n];
for (int i = 0; i < n; i++) {
System.out.println("Subject " + (i + 1));
System.out.print("Title: ");
String title = buf.readLine();
System.out.print("Internal Marks (Out of 30): ");
double internal = Double.parseDouble(buf.readLine());
System.out.print("Theory Marks (Out of 70): ");
double theory = Double.parseDouble(buf.readLine());
subs[i] = new Subject(title, internal, theory, internal +
theory);
Total+=internal + theory;
}
Student8c Student2 = new Student8c(name, stream, college, rollNo,
semester, Total );
System.out.println("\n------------Student
Information-----------");
System.out.println(Student1.toString());
System.out.println(Student2.toString());
}
}
Output:
Question: 9) Design a class to represent a Bank Account. Include the following things:
Fields
● Name of the depositor
● Address of the depositor
● Account number
● Balance amount in the account
Methods
● To assign initial values
● To deposit an amount
● To withdraw an amount after checking balance
● To display the name, address and balance of a customer.
From main create object and call these methods.
Code:
import java.util.Scanner;
System.out.println("-------------------------------Details----------------
--------------");
System.out.println("Name: "+account.name);
System.out.println("Address: "+account.address);
System.out.println("Account Number: "+account.accountNum);
System.out.println("Balance: "+account.balance);
}
case 2:System.out.println("------------------------Using
Existing Account------------------------");
System.out.println("Enter the bank account number: ");
String number = new Scanner(System.in).nextLine();
int flag=0, index=0;
for(int i = 0; i < pos; i++){
if (accounts[i].accountNum.equals(number) &&
accounts[i].accountNum != null){
flag=1;
index=i;
}
}
if(flag == 0){
System.out.println("Account Not Found");
}
else{
int choice2=0;
System.out.println("------------------------Accessing Account:
"+number+"---------------------------");
while(choice2!=4){
System.out.println("\nWhat do you want to
do?");
System.out.println(" 1.Deposit an amount\n
2.Withdraw an amount\n 3.Show Details\n 4.Exit Account");
choice2 = new Scanner(System.in).nextInt();
switch (choice2){
case 1:System.out.print("Enter your
deposit amount: ");
double depositAmount =
input.nextDouble();
deposit(accounts[index],
depositAmount);
System.out.println("Amount
Deposited!");
break;
case 3:showDetails(accounts[index]);
break;
default:System.out.println("Invalid
input");
break;
}
}
}
break;
default:System.out.println("Invalid input");
break;
}
System.out.println("Do you want to continue? Enter 1 for Yes,
0 for No");
c = input.nextInt();
}
}
}
Output: