0% found this document useful (0 votes)
5 views7 pages

Labsheet2 Solutions

Uploaded by

nooraamira22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Labsheet2 Solutions

Uploaded by

nooraamira22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 7

Labsheet 2 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

using Command Line Arguments.

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:

a) Create a Class called Student with given attributes(data fields)

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{

Scanner input = new Scanner(System.in);

String name;

int roll;

String branch;

int sem;

void read(){

System.out.println("Enter Student's Name, Roll number,Branch and Semester");

name = input.next();

roll = input.nextInt();

branch = input.next();

sem = input.nextInt();

Student1(){

name = "";
roll = 0;

branch = "";

sem = 0;

Student1(String name,int roll,String branch,int sem){

this.name = name;

this.roll = roll;

this.branch = branch;

this.sem = sem;

Student1(String n, int r){

System.out.println("Enter Student's Branch and Semester");

name = n;

roll = r;

branch = input.next();

sem = input.nextInt();

void display(){

System.out.println("Student's Name is " +name);

System.out.println("Student's Roll no. is "+roll);

System.out.println("Student's Branch is " +branch);

System.out.println("Student's Semester is " +sem);

public class lab2 {

public static void main(String[] args) {

Student1 s1 = new Student1();

Student1 s2 = new Student1("Takeshi",34,"LAW",2);

Student1 s3 = new Student1("UziVert",85);


Student1 s4 = new Student1();

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{

void Areas(int r){

System.out.printf("Area of a Shape:%2f\n",(Math.PI*r*r));

void Areas(int l, int b){

System.out.println("Area of the Shape is: "+(l*b));

void Areas(float l, float b){


System.out.println("Area of the Shape is: "+(0.5f*l*b));

public class lab2 {

public static void main(String[] args) {

AreaOfShapes s1 = new AreaOfShapes();

s1.Areas(5);

s1.Areas(7,11);

s1.Areas(5.3f,1.2f);

3. Four employees decided to celebrate their manager’s birthday. Everyone contributed

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.

a) Every employee contribution and who has contributed the highest.

b) Amount spent on each item. Help an employee by using constructor overloading, static

variable and static function to demonstrate the same.

import java.util.Scanner;

class Employees {

Scanner sc = new Scanner(System.in);

static double cake = 0.25;

static double gift = 0.25;

static double food = 0.50;

double cont = 0;

Employees(int i) {

System.out.printf("Enter the contribution of Employee %d: ", i);


double temp = sc.nextDouble();

if (temp > 500) {

cont = temp;

else {

System.out.println("PLEASE CONTRIBUTE MORE");

temp = sc.nextDouble();

cont = temp;

Employees(Employees p){

cont=p.cont;

static void Spent(double amount){

System.out.println("Total amount collected is: "+amount);

System.out.println("Amount spent on cake is: "+(cake*amount));

System.out.println("Amount spent on gift is: "+(gift*amount));

System.out.println("Amount spent on food is: "+(food*amount));

public class lab2 {

public static void main(String[] args) {

Employees e1=new Employees(1);

Employees e2=new Employees(2);

Employees e3=new Employees(e1); //copy constructor

Employees e4=new Employees(4);

if(e1.cont>e2.cont && e1.cont>e4.cont){

System.out.println("Employee 1 and 3 has highest contribution");

}
else if(e2.cont>e1.cont && e1.cont>e4.cont){

System.out.println("Employee 2 has highest contribution");

else{

System.out.println("Employee 4 has highest contribution");

double amount= e1.cont+ e2.cont+ e3.cont+ e4.cont;

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.

Demonstrate program to access of both inner and outer inner members.

class OuterClass{

private int data=74;

class Inner{

int info=15;

void display(){

System.out.println("Data member of Outer class is: "+data);

System.out.println("Data member of Inner class is: "+info);

public class lab2 {

public static void main(String[] args) {

OuterClass o1=new OuterClass();

OuterClass.Inner in = o1.new Inner();

in.display();
}

You might also like