modify according to the instruction in jgrasp The “Person Program” – see Instruc
ID: 3852915 • Letter: M
Question
modify according to the instruction in jgrasp The “Person Program” – see Instructions for modifications of this program below. class Person { //Instance variables (data or "state") String name; int age; // Classes can contain // 1. Data // 2. Subroutines (called methods in Java void speak() { System.out.println("Hello"); System.out.println("My name is: " +name +" and I am " +age + " years old"); } } public class App { public static void main(String[] args) { // Create a Person object using the person class Person person1 = new Person(); person1.name = "Joe Bloggs"; person1.age = 37; person1.speak(); // Create a second Person object Person person2 = new Person(); person2.name = "Sarah Smith"; person2.age = 20; System.out.println(person1.name); } }
Instructions a. Create a third (3rd) person object and give them a name and age.
b. Create a city where each person lives
c. Create Savings Account balance for each person. Make sure it shows dollars and cents; it is not necessary to use the $ for currency.
d. Create a “write “ method (a behavior) that each person is capable of carrying out.
e. Have person3 who has “commitment issues” write the poem “Roses” on p. 64 of your text in the format specified in the text..
f. Write a method for counting and have person1 count from 1 to 5, three times.
Explanation / Answer
App.java
public class App
{
public static void main(String[] args)
{
// Create a Person object using the person class
Person person1 = new Person();
person1.name = "Joe Bloggs";
person1.age = 37;
person1.city = "Paris";
person1.speak();
person1.count();
person1.count();
person1.count();
// Create a second Person object
Person person2 = new Person();
person2.name = "Sarah Smith";
person2.age = 20;
person2.city = "Paris";
// Create a 3rd Person object
Person person3 = new Person();
person3.name = "Joe root";
person3.age = 25;
person3.city = "Paris";
person3.write("Roses");
}
}
Person.java
class Person
{
//Instance variables (data or "state")
String name; int age; String city; double balance; String poem;
// Classes can contain // 1. Data // 2. Subroutines (called methods in Java
void speak()
{
System.out.println("Hello");
System.out.println("My name is: " + name +" and I am " +age + " years old");
}
void write(String p)
{
this.poem = p;
}
void count()
{
for (int i = 1;i<=5 ;i++ )
{
System.out.print(i + " ");
}
System.out.println("");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.