0% found this document useful (0 votes)
6 views

OOP Lab2 Exercise Solution

Oop

Uploaded by

abinetmelkamu165
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

OOP Lab2 Exercise Solution

Oop

Uploaded by

abinetmelkamu165
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Solution for OOP Lab2 Exercise

1.
package studentProfile;
import java.util.Formatter;
import java.util.Scanner;
public class Student {
private String id, firstName, secondName, sex, department;
private static int noOfStudents = 0;
private int age;
private float gpa;
public Student(String id, String fn, String sn, String sex, String dep,
int age, float gpa){
this.id = id;
firstName = fn;
secondName = sn;
this.sex = sex;
department = dep;
this.age = age;
this.gpa = gpa;
noOfStudents++;
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("How many Students you want to Register? ");
int number = input.nextInt();
String id[] = new String[number];
String fn[] = new String[number];
String sn[] = new String[number];
String sex[] = new String[number];
String dep[] = new String[number];
int age[] = new int[number];
float gpa[] = new float[number];
for(int i=0;i<number;i++){
System.out.println("Entre Student ID: ");
id[i] = input.next();
System.out.println("Entre First Name: ");
fn[i] = input.next();
System.out.println("Entre Second Name: ");
sn[i] = input.next();
System.out.println("Entre Sex: ");
sex[i] = input.next();
System.out.println("Entre Department: ");
dep[i] = input.next();
System.out.println("Entre Age: ");
age[i] = input.nextInt();
System.out.println("Entre GPA: ");
gpa[i] = input.nextFloat();
}
Student stud[] = new Student[number];
Formatter format1 = new Formatter();
format1.format("%1s %15s %15s %15s %15s %15s %15s %15s", "No","First Name",
"Second Name", "ID","Department","Age","Sex", "GPA");
System.out.println(format1);
for(int i=0;i<number;i++){
Formatter format2 = new Formatter();
stud[i] = new Student(id[i],fn[i],sn[i],sex[i],dep[i],age[i],gpa[i]);
format2.format("%1s %15s %15s %15s %15s %15s %15s %15s", (i+1),
stud[i].firstName, stud[i].secondName, stud[i].id,
stud[i].department, stud[i].age, stud[i].sex, stud[i].gpa);
System.out.println(format2);
}
System.out.println("Number of Students Registered are "+noOfStudents);
System.out.println("Number of Students Registered are "+
Student.noOfStudents);
}
}

2.
package hunger;
import java.util.Scanner;
public class HungerPrediction {
private double polStat, devpLevel, rainfall, prevYearHarv, cgdp, popuSize;
public HungerPrediction(double polStat, double devpLevel, double rainfall,
double prevYearHarv, double cgdp, double popuSize){
this.polStat = polStat;
this.devpLevel = devpLevel;
this.rainfall = rainfall;
this.prevYearHarv = prevYearHarv;
this.cgdp = cgdp;
this.popuSize = popuSize;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
double polStat, devpLevel, rainfall, prevYearHarv, cgdp, popuSize;
System.out.println("Enter the Political Stability of a Country: ");
polStat = in.nextDouble();
System.out.println("Enter the Development Level of a Country: ");
devpLevel = in.nextDouble();
System.out.println("Enter the Annual Rainfall of a Country: ");
rainfall = in.nextDouble();
System.out.println("Enter the Previous Year Harvest of a Country: ");
prevYearHarv = in.nextDouble();
System.out.println("Enter the GDP of a Country: ");
cgdp = in.nextDouble();
System.out.println("Enter the Population Size of a Country: ");
popuSize = in.nextDouble();
HungerPrediction hp = new HungerPrediction(polStat, devpLevel, rainfall,
prevYearHarv, cgdp, popuSize);
double hugPred = (hp.polStat+hp.devpLevel+hp.rainfall+hp.prevYearHarv)/
(hp.cgdp*0.1)*hp.popuSize;
System.out.println("The Hunger Prediction of a Country is "+hugPred);
}
}

3. Do Question #3 by Yourself using Array based on #1

4. A) One class with one file name

package oneClassWithOneFileName;
public class Circle {
private double radius;
private static int noOfObjects = 0;
public Circle(){
radius = 5.5;
noOfObjects++;
}
public Circle(double radius){
this.radius = radius;
noOfObjects++;
}
public double getRadius(){
return radius;
}
public void setRadius(double rad){
radius = rad;
}
public static int getNoOfObjects(){
return noOfObjects;
}
public double getArea(){
return radius*radius*Math.PI;
}
public double getPerimeter(){
return 2*radius*Math.PI;
}
public double getDiameter(){
return 2*radius;
}
public static void main(String[] args){
Circle c1 = new Circle();
Circle c2 = new Circle(7.5);
System.out.println("Radius of Circle 1 is "+c1.radius);
System.out.println("Radius of Circle 1 is "+c1.getRadius());
System.out.println("Area of Circle 1 is "+c1.getArea());
System.out.println("Perimeter of Circle 1 is "+c1.getPerimeter());
System.out.println("Diameter of Circle 1 is "+c1.getDiameter());

c1.setRadius(15.5);
System.out.println("Radius of Circle 1 is "+c1.radius);
System.out.println("Radius of Circle 1 is "+c1.getRadius());
System.out.println("Area of Circle 1 is "+c1.getArea());
System.out.println("Perimeter of Circle 1 is "+c1.getPerimeter());
System.out.println("Diameter of Circle 1 is "+c1.getDiameter());

System.out.println("Radius of Circle 1 is "+c2.radius);


System.out.println("Radius of Circle 1 is "+c2.getRadius());
System.out.println("Area of Circle 1 is "+c2.getArea());
System.out.println("Perimeter of Circle 1 is "+c2.getPerimeter());
System.out.println("Diameter of Circle 1 is "+c2.getDiameter());

System.out.println("The Number of Objects Created is "+noOfObjects);


System.out.println("The Number of Objects Created is "+
Circle.getNoOfObjects());
}
}

B) Two class with one file name


package twoClassWithOneClass;
public class Circle {
private double radius;
private static int noOfObjects = 0;
public Circle(){
radius = 5.5;
noOfObjects++;
}
public Circle(double radius){
this.radius = radius;
noOfObjects++;
}
public double getRadius(){
return radius;
}
public void setRadius(double rad){
radius = rad;
}
public static int getNoOfObjects(){
return noOfObjects;
}
public double getArea(){
return radius*radius*Math.PI;
}
public double getPerimeter(){
return 2*radius*Math.PI;
}
public double getDiameter(){
return 2*radius;
}
}

class TestCircle{
public static void main(String[] args){
Circle c1 = new Circle();
Circle c2 = new Circle(7.5);

System.out.println("Radius of Circle 1 is "+c1.getRadius());


System.out.println("Area of Circle 1 is "+c1.getArea());
System.out.println("Perimeter of Circle 1 is "+c1.getPerimeter());
System.out.println("Diameter of Circle 1 is "+c1.getDiameter());

c1.setRadius(15.5);
System.out.println("Radius of Circle 1 is "+c1.getRadius());
System.out.println("Area of Circle 1 is "+c1.getArea());
System.out.println("Perimeter of Circle 1 is "+c1.getPerimeter());
System.out.println("Diameter of Circle 1 is "+c1.getDiameter());

System.out.println("Radius of Circle 1 is "+c2.getRadius());


System.out.println("Area of Circle 1 is "+c2.getArea());
System.out.println("Perimeter of Circle 1 is "+c2.getPerimeter());
System.out.println("Diameter of Circle 1 is "+c2.getDiameter());

System.out.println("The Number of Objects Created is "+


Circle.getNoOfObjects());
}
}

C) Two class with Two file name


package twoClassWithTwoClass;
public class Circle {
private double radius;
private static int noOfObjects = 0;
public Circle(){
radius = 5.5;
noOfObjects++;
}
public Circle(double radius){
this.radius = radius;
noOfObjects++;
}
public double getRadius(){
return radius;
}
public void setRadius(double rad){
radius = rad;
}
public static int getNoOfObjects(){
return noOfObjects;
}
public double getArea(){
return radius*radius*Math.PI;
}
public double getPerimeter(){
return 2*radius*Math.PI;
}
public double getDiameter(){
return 2*radius;
}
}

package twoClassWithTwoClass;
import twoClassWithTwoClass.Circle;
public class TestCircle {
public static void main(String[] args){
Circle c1 = new Circle();
Circle c2 = new Circle(7.5);

System.out.println("Radius of Circle 1 is "+c1.getRadius());


System.out.println("Area of Circle 1 is "+c1.getArea());
System.out.println("Perimeter of Circle 1 is "+c1.getPerimeter());
System.out.println("Diameter of Circle 1 is "+c1.getDiameter());

c1.setRadius(15.5);
System.out.println("Radius of Circle 1 is "+c1.getRadius());
System.out.println("Area of Circle 1 is "+c1.getArea());
System.out.println("Perimeter of Circle 1 is "+c1.getPerimeter());
System.out.println("Diameter of Circle 1 is "+c1.getDiameter());

System.out.println("Radius of Circle 1 is "+c2.getRadius());


System.out.println("Area of Circle 1 is "+c2.getArea());
System.out.println("Perimeter of Circle 1 is "+c2.getPerimeter());
System.out.println("Diameter of Circle 1 is "+c2.getDiameter());

System.out.println("The Number of Objects Created is "+


Circle.getNoOfObjects());
}
}

5.

package book;
import java.util.Scanner;
public class Book {
public String title;
private String authorName;
private String isbn;
protected double price;
public Book(String title, String author, String isbn, double price){
this.title = title;
setAuthorName(author);
setIsbn(isbn);
this.price = price;
}
private void hasSoldAt(){
System.out.println("Introduct to Java Programming Book has been sold at
price "+price);
}
public String getAuthorName(){
return authorName;
}
public void setAuthorName(String author){
char firstLetter = author.charAt(0);
boolean cap = Character.isUpperCase(firstLetter);
if(cap)
authorName = author;
else
System.out.println("The Author Name doesn't begin with Capital
Letter");
}
public String getIsbn(){
return isbn;
}
public void setIsbn(String isbn){
if(isbn.length()==10)
this.isbn = isbn;
else
System.out.println("The length of the ISBN Code should be Exactly 10 ");
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String title, author, isbn;
double price;
System.out.println("Enter the Title of the Book: ");
title = in.nextLine();
System.out.println("Enter the Author of the Book: ");
author = in.nextLine();
System.out.println("Enter the ISBN Code of the Book: ");
isbn = in.nextLine();
System.out.println("Enter the Price of the Book: ");
price = in.nextDouble();
Book book = new Book(title, author, isbn, price);
System.out.println("The title of the Book is "+book.title);
System.out.println("The Author Name of the Book is "+book.getAuthorName());
System.out.println("The ISBN Code of the Book is "+book.getIsbn());
System.out.println("The Price of the Book is "+book.price);
}
}

You might also like