Program Name :BCS
Course Code: CSC 2513
Course Name: Programming Fundamentals
Lab Sheet: 4
Date of Submission: 5th May 2021
Submitted By: Submitted To:
Student Name: Udit Kumar Mahato Faculty Name: Mr. Prakash Chandra
IUKL ID: 042002900006 Department: PO office
Semester: Second Semester
Intake: Sept 2020
1.Write a class named Student. This class should contain some member variable(such as: name, age,
rollno...). Define two method as mentioned below
a.InitializeStudent(): This method will initialize object by taking all required variable as argument and
assign it to the member variables.
b.printData(): this method will print all the data of student in formatted output format.
Ans:-
class Student{
String name;
int age;
int rollno;
void initializeStudent(String n,int a,int r){
name=n;
age=a;
rollno=r;
}
void printData(){
System.out.println("Name : "+name);
System.out.println("Age : "+age);
System.out.println("Roll no : "+rollno);
}
}
public class StudentClass {
public static void main(String[] args) {
Student stu=new Student();
stu.initializeStudent("Princu",9,1);
stu.printData();
}
}
Output:-
2.Define a class Student as described below:
Instance variables:
name, age, marks in three subjects (m1, m2, m3), maximum and average.
Methods:
i. A parameterized constructor to initialize the instance variables.
ii. To accept the details of a student.
iii. To compute the average and minimum out of the three marks.
iv. To display the name, age, marks in the three subjects, minimum and average.
v. Write a main method to create an object of the class and call the above methods.
Ans:-
class Students{
String name;
double age,marks1,marks2,marks3,minimum,average;
Students(String n,double a,double m1,double m2,double m3){
name=n;
age=a;
marks1=m1;
marks2=m2;
marks3=m3;
}
void getAvg(){
average=(marks1+marks2+marks3)/3;
System.out.println("Average of the marks : "+average);
}
void getMin(){
minimum = Math.min(Math.min(marks1,marks2),marks3);
System.out.println("Minimum marks : "+minimum);
}
void display(){
System.out.println("Name : "+name);
System.out.println("Age : "+age);
System.out.println("Marks 1 : "+marks1);
System.out.println("Marks 2 : "+marks2);
System.out.println("Marks 3: "+marks3);
}
}
public class StudMark {
public static void main(String[] args) {
Students s=new Students("sumit",15,57,98,52);
s.display();
s.getAvg();
s.getMin();
}
}
Output:
3.Write a class Circle with a parameterized constructor. If no parameters are passed then its default
constructor should be invoke parameterized constructor with default values. A circle is defined using
radius and circumference.
Ans:-
import java.lang.Math.*;
class Circle{
double radius;
double circumference;
Circle(){
radius=7.98;
System.out.println("Default Radius Value :
"+radius+"\n"+"Circumference taking default radius value :
"+2*Math.PI*radius);
}
Circle(double r){
radius=r;
System.out.println("Given Radius Value : "+radius+"\n"+"Circumference
taking given radius value : "+2*Math.PI*radius);
}
}
public class circleCircum {
public static void main(String[] args) {
Circle s1=new Circle();
Circle s2=new Circle(198.7);
}
}
Output:-
4 .Write a program to implement a Book class that stores the details of a book namely, bookcode,
name of the book, name of the author(s) and price. The class has methods to display any of the details
individually.
Ans:-
class Book{
int bookcode;
String bookname;
String bookauthor;
double price;
void bookDetails(int bc,String bn,String ba,double p){
bookcode=bc;
bookname=bn;
bookauthor=ba;
price=p;
}
void getBookCode(){
System.out.println("BOOK CODE : "+bookcode);
}
void getBookName() {
System.out.println("BOOK NAME : "+bookname);
}
void getBookAuthor(){
System.out.println("BOOK AUTHOR : "+bookauthor);
}
void getBookPrice(){
System.out.println("BOOK PRICE : "+price);
}
}
public class bookClass {
public static void main(String[] args) {
Book d1=new Book();
d1.bookDetails(73547,"The power of subconscious mind","Dr. Joseph
Murphy",4500);
d1.getBookCode();
d1.getBookName();
d1.getBookAuthor();
d1.getBookPrice();
}
}
Output:
5.Implement a class called Dimension based on the following information:
Constructors
Dimension(double length, double width, double height)
Dimension(double side)
Methods
double volume() // length*width*height
double area() // 2*(length*width+width*height+height*length)
6 .Make all the instance variables private so that they can be accessed only by the methods defined
within the class. Make the methods public. Test your program.
7 .Modify the implementation of area() given in the previous question using private
methods, faceArea(), topArea() and sideArea(). [Often private methods are helping methods that
public methods use, but are not to be used outside the class.] Test your program.
8.Add a new constructor to the Dimension class created in question 1 as
Dimension(Dimension dim)
This constructor creates a new Dimension object with identical dimensions as the old
Dimension object. The old object is not changed.
Ans:-
//No .6,7 and 8 are coded in single program below
class Dimension{
private double length;
private double width;
private double height;
private double side;
Dimension(double l,double w,double h){
length=l;
width =w;
height=h;
}
Dimension(double s){
side=s;
}
Dimension(Dimension dim){
length=4;
width=25;
height=50;
System.out.println("Volume while values are default :"+ length *
width * height);
System.out.println("Area while values are default :"+
2*(length*width+width*height+height*length);
}
public double volume(){
double volume = length * width * height;
return volume;
}
public double area(){
double area=2*(length*width+width*height+height*length);
return area;
}
private double toparea(){
return(length*height);
}
private double facearea(){
return(length*width);
}
private double sidearea(){
return(width*height);
}
}
public class Dimen {
public static void main(String[] args) {
Dimension d1= new Dimension(25,45,67);
Dimension d2 = new Dimension();
System.out.println("Volume : "+d1.volume()+"\n"+"Area : "+ d1.area());
System.out.println("Volume : "+d1.volume()+"\n"+"Area : "+
d1.facearea());
System.out.println("Volume : "+d1.volume()+"\n"+"Area : "+
d1.sidearea());
System.out.println("Volume : "+d1.volume()+"\n"+"Area : "+
d1.toparea());
System.out.println(d2.Dimension());
}
}
Output:-
9. Design a class to represent a bank account. Include the following members:
Fields/Data members
Name of the depositor
Account number
Type of account
Balance amount in the account
Methods
Constructor(s)
To assign initial values
To deposit an amount
To withdraw an amount after checking balance
To display the name and balance.
Test the bank account class by performing all actions defined in BankAccount class.
Ans:
import java.util.Scanner;
class Data{
String name;
double accountnum;
String type;
double amount;
double depositemoney;
double withdraw;
void Data(String name,double accountnum, String type, double amount){
this.name= name;
this.accountnum =accountnum;
this.type = type;
this.amount= amount;
}
void deposite(double depositemoney){
this.depositemoney = depositemoney;
double newmoney = this.amount+this.depositemoney;
System.out.println(newmoney);
this.amount =newmoney;
System.out.println("your new balance is"+ ""+ newmoney);
}
void withdraw(){
double newmoneys =this.amount-withdraw;
this.amount = newmoneys;
System.out.println("your new amount "+newmoneys);
}
void details(){
System.out.println("account holder name is:" + this.name);
System.out.println("");
System.out.println("your account type:"+ this.type);
System.out.println("");
System.out.println("your current money:"+this.amount);
System.out.println("");
public class bankAcc {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Data obj = new Data();
obj.name ="Sagar";
obj.accountnum=123;
obj.type ="saving";
obj.amount = 5000;
System.out.println("enter your pin number");
int pin =sc.nextInt();
if (pin==123){
char choice;
do{
while(true)
{
System.out.println("Automated Teller Machine");
System.out.println("Choose 1 for Withdraw");
System.out.println("Choose 2 for Deposit");
System.out.println("Choose 3 for Check Balance");
System.out.println("choose 4 details");
System.out.println("Choose 5 for EXIT");
System.out.print("Choose the operation you want to
perform:");
int n = sc.nextInt();
switch(n)
{
case 1:
if (obj.amount>=obj.withdraw){
System.out.println("enter the anount you want
to withdraw");
obj.withdraw = sc.nextDouble();
obj.withdraw();
}
else{
System.out.println("low balance");
}
break;
case 2:
System.out.println("enter the money you want to
deposite");
obj.depositemoney = sc.nextDouble();
obj.deposite(obj.depositemoney);
break;
case 3:
System.out.println("your balane is"+obj.amount);
System.out.println("");
break;
case 4:
System.out.println("your details is");
obj.details();
break;
case 5:
System.exit(0);
}
System.out.println("do you want to continue(y/N)");
choice =sc.next().charAt(0);
}
}while(choice=='y'||choice =='Y');
else{
System.out.println("sorry");
}
}
}
output: