Download Assignment02

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introductory Programming
Assignment 02
Teaching Assistant: Jesper Mouritzen
Name
Ravikumar Kemapaih
Assignment02
-1-
Ravikumar Kempaiah
OUTPUT:
Input new Name:
Harshith Paul
Input the Persons height in Cms:
170
Person 1 : John Doe, 175 Cm.
Person 2 : Ravikumar, 176 Cm.
Person 3 : Harshith Paul, 170 Cm.
The average height of 3 persons is : 173.67CM
MODIFICATION
Enter new height value in Cms:
176
UPDATED PERSONS INFORMATION
Person 1 : John Doe, 175 Cm.
Person 2 : Ravikumar, 176 Cm.
Person 3 : Harshith Paul, 176 Cm.
The average height of 3 persons is : 175.67 Cm
Screen shot
Assignment02
-2-
Ravikumar Kempaiah
SOURCE CODE:
Person.java
/* ***************************************************************
* Person.java Author: Ravikumar Kempaiah
Assignment01
* Person Class without main method
/************************************************************** */
public class Person {
//Define attributes person and height
String person;
int height;
//***************************************************************
// Constructor: Sets name and hegiht
//***************************************************************
public Person(){
person = "John Doe";
height = 175;
}
//**************************************************************
//
Constructor: Takes parameters name and height
//**************************************************************
public Person(String name, int height1){
person = name;
height = height1;
}
//**************************************************************
// Name mutator
//**************************************************************
public void setName(String navn){
person = navn;
}
//**************************************************************
// Name accessor
//**************************************************************
public String getName(){
return person;
}
//**************************************************************
// Persons height mutator
//**************************************************************
Assignment02
-3-
Ravikumar Kempaiah
public void setHeight(int tall){
height = tall;
}
//**************************************************************
// Persons height accessor
//**************************************************************
public int getHeight(){
return height;
}
//**************************************************************
// returns string representation of Persons name and height
//**************************************************************
public String toString(){
return person+ ", " +height+" Cm.";
}
}
TestPerson.java
/* ***************************************************************
* TestPerson.java Author: Ravikumar Kempaiah Assignment01
* TestPerson Class with main method
* Creats 3 Person objects and displays there name, height and average of their height
/************************************************************** */
import java.util.Scanner;
public class TestPerson {
public static void main(String args[]){
Person P1, P2, P3; // declare 3 Person objects
float sum, average;
String InputName;
int InputHeight, chgHeight;
// Instatiate 3 Person objects
P1 = new Person();// Instatiate using constructor without parameter
P2 = new Person("Ravikumar", 176); // with parameter
// Read name of the person through input
Scanner scan = new Scanner(System.in);
System.out.println("Input new Name: ");
InputName = scan.nextLine();
// Read height of the person through input
Scanner scanHeight = new Scanner(System.in);
Assignment02
-4-
Ravikumar Kempaiah
System.out.println("Input the Persons height in Cms: ");
InputHeight = scanHeight.nextInt();
// instatiate using the input values as parameters
P3 = new Person(InputName, InputHeight);
// Print the contents (name and height) of each person
System.out.println("Person 1 : "+P1.toString());
System.out.println("Person 2 : "+P2.toString());
System.out.println("Person 3 : "+P3.toString());
// Calculate and display the average height of the persons
sum = P1.getHeight()+ P2.getHeight()+ P3.getHeight();
average = sum/3;
// Round off the float vale to 2 decimal places
DecimalFormat dft = new DecimalFormat("0.##");
System.out.println("The average height of 3
dft.format(average)+ " Cm \n\n");
persons
is
:
"
+
// Modify the height of the 3rd person through user input
System.out.println("MODIFICATION ");
System.out.println("Enter new height value in Cms: ");
Scanner scanHt = new Scanner (System.in);
chgHeight = scanHt.nextInt();
P3.setHeight(chgHeight);
// Print the updated contents of the persons
System.out.println("UPDATED PERSONS INFORMATION ");
System.out.println("Person 1 : "+P1.toString());
System.out.println("Person 2 : "+P2.toString());
System.out.println("Person 3 : "+P3.toString());
// find the new average height of the persons
sum = P1.getHeight()+ P2.getHeight()+ P3.getHeight();
average = sum/3;
System.out.println("The average height of 3 persons is : " + dft.format(average) + "
Cm");
}
}
Assignment02
-5-
Ravikumar Kempaiah