Labsheet2 Solutions
Labsheet2 Solutions
1. A teacher asked the class leader to prepare the list of student details (name, Roll number, Branch
and Sem) of his section by using read method and also to display the details of the students using
java program.
Q1 Demonstrate the application by creating, reading and displaying the one student of the section
Q2 Demonstrate the application by creating, reading and displaying the three students of the
section using Scanner class. Q3 Demonstrate the same application using this keyword
Requirements:
b) Add the Method called “void Read()” to read the values of the attributes of the class student
c) Also Add the Method called “void Display()” to display the values read for the attributes of the
class student.
import java.util.Scanner;
class Student1{
String name;
int roll;
String branch;
int sem;
void read(){
name = input.next();
roll = input.nextInt();
branch = input.next();
sem = input.nextInt();
Student1(){
name = "";
roll = 0;
branch = "";
sem = 0;
this.name = name;
this.roll = roll;
this.branch = branch;
this.sem = sem;
name = n;
roll = r;
branch = input.next();
sem = input.nextInt();
void display(){
System.out.println(" ");
s1.read();
// s2.read();
// s3.read();
s1.display();
s2.display();
s3.display();
s4.name = args[0];
s4.roll = Integer.parseInt(args[1]);
s4.branch = args[2];
s4.sem = Integer.parseInt(args[3]);
s4.display();
2. A teacher gave a project to student in the classroom to find the area of different shapes. A
student has to find the area of different shapes based on the choice of different parameters like:
area of the shape by passing one parameter, area of the shape by passing two parameters, area of
the shape which has all sides are equal by passing one parameter using overloading and display
the results.
class AreaOfShapes{
System.out.printf("Area of a Shape:%2f\n",(Math.PI*r*r));
s1.Areas(5);
s1.Areas(7,11);
s1.Areas(5.3f,1.2f);
minimum of 500 rupees. Employee 1 and Employee 3 contributed the same amount. 25% of
amount is spent on cake, 25% of amount is spent on gift and 50% of money is spent on food. One
of the employees explained the plan and action of the event by explaining the following to his/her
team.
b) Amount spent on each item. Help an employee by using constructor overloading, static
import java.util.Scanner;
class Employees {
double cont = 0;
Employees(int i) {
cont = temp;
else {
temp = sc.nextDouble();
cont = temp;
Employees(Employees p){
cont=p.cont;
}
else if(e2.cont>e1.cont && e1.cont>e4.cont){
else{
Employees.Spent(amount);
4. Develop a java program with two classes an Outer and Inner class. Each of the class have one
data member, the inner class has member function which displays the data members.
class OuterClass{
class Inner{
int info=15;
void display(){
in.display();
}