Swapnanil Dutta Roll 12 Day 8 PCC-CS593

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

Question:​ 8.

​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;

​Student​(String ​Name​,​int​ ​rollNo​,String ​Stream​,String ​College​){


​this​.Name​=​Name;
​this​.rollNo​=​rollNo;
​this​.Stream​=​Stream;
​this​.College​=​College;
}

​void​ ​display​(){
System.out.​println​(​"​\n​Name: "​+​Name​+​" ​\n​Roll No: "​+​rollNo​+​"
\n​Stream: "​+​Stream​+​" ​\n​College: "​+​College);

}
​public​ ​static​ ​void​ ​main​(​String​[]​args​){
System.out.​println​(​"-------------------------Input Student
Details--------------------------"​);
Scanner st​=new​ ​Scanner​(System.in);
System.out.​print​(​"​\n​Enter 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​(​"​\n​Enter 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​ ​"​\n​Name: "​ ​+​ name ​+​ ​"​\n​Roll: "​ ​+​ roll ​+​ ​"​\n​Stream: "​ ​+
stream ​+​ ​"​\n​Total "​ ​+​ 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​ ​"​\n​Student Name = "​+​this​.name ​+​ ​"​\n​Roll no = "​ ​+
this​.rollNo ​+​ "
​ ​\n​Stream = "​ ​+​ ​this​.stream ​+​ ​"​\n​Semester = "​ ​+
this​.semester + ​ ​ ​"​\n​College = "
​+​ ​this​.college ​+​ ​"​\n​Total 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;

public​ ​class​ ​BankAccount​{


String name, address, accountNum;
​double​ balance;
​//Non parameterized constructor for convenience of initialization
​BankAccount​() {
​this​.name ​=​ ​null​;
​this​.address ​=​ ​null​;
​this​.accountNum ​=​ ​null​;
​this​.balance ​=​ ​0.0​;
}
​//Parameterized constructor for account setup
​BankAccount​(String ​name​, String ​address​, String ​accountNum​) {
​this​.name ​=​ name;
​this​.address ​=​ address;
​this​.accountNum ​=​ accountNum;
​this​.balance ​=​ ​0.0​;
}
​//Deposit amount to balance
​static​ ​void​ ​deposit​(BankAccount ​account​, ​double​ ​deposit​) {
account.balance​+=​deposit;
}
​//Withdraw amount from balance
​static​ ​void​ ​withdraw​(BankAccount ​account​, ​double​ ​withdrawal​){
​if​ (account.balance ​>=​ withdrawal){
account.balance ​-=​ withdrawal;
System.out.​printf​(​"Withdrew %.2f from account. Balance left:
%.2f"​,withdrawal,account.balance);
System.out.​println​(​""​);
}
​else​{
System.out.​println​(​"Withdrawal not possible, balance too
low."​);
}
}
​//Show account details
​static​ ​void​ ​showDetails​(BankAccount ​account​){

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);
}

​public​ ​static​ ​void​ ​main​(​String​[] ​args​) {


String name, address, accountNum;
​double​ balance;
Scanner input = ​ ​ ​new​ ​Scanner​(System.in);
​BankAccount​[] accounts ​=​ ​new​ ​BankAccount​[​10​];
//Initialize account array
​int​ pos ​=​ ​0​;
//Initialize pos of array
​int​ c​=​1​;
//Choice variable
​while​(c ​!=​ ​0​){
System.out.​println​(​"Enter your choice:"​);
System.out.​println​(​" 1.Create an account​\n​ 2.Use an existing
account"​);
​int​ choice1 ​=​ input.​nextInt​();
​switch​ (choice1) {
​case​ ​1​:​System.out.​println​(​"-----------------------Creating
New Account--------------------------"​);
System.out.​println​(​"Enter your name: "​);
name ​=​ ​new​ ​Scanner​(System.in).​nextLine​();
System.out.​println​(​"Enter your address: "​);
address ​=​ ​new​ ​Scanner​(System.in).​nextLine​();
accountNum ​=​ ​"112254125"​+​String.​valueOf​(pos);
accounts[pos] ​=​ ​new​ ​BankAccount​(name, address,
accountNum);
​showDetails​(accounts[pos]);
pos​++​;
​break​;

​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​(​"​\n​What 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​ ​2​:​System.out.​print​(​"Enter your


withdrawal amount: "​);
​double​ withdrawal ​=
input.​nextDouble​();
​withdraw​(accounts[index], withdrawal);
​break​;

​case​ ​3​:​showDetails​(accounts[index]);
​break​;

​case​ ​4​:​System.out.​println​(​"Exiting Account


"​+​number);
​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:

You might also like