Java Lab 4
Java Lab 4
Lab Sheet: 4
Student Name: Udit Kumar Mahato Faculty Name: Mr. Prakash Chandra
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:-
name, age, marks in three subjects (m1, m2, m3), maximum and average.
Methods:
i. A parameterized constructor to initialize the instance variables.
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:
Methods
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.
This constructor creates a new Dimension object with identical dimensions as the old
Ans:-
}
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:-
Type of account
Methods
Constructor(s)
To deposit an amount
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 withdraw(){
}
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("");
obj.name ="Sagar";
obj.accountnum=123;
obj.type ="saving";
obj.amount = 5000;
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: