Unit 5 demo - Person
//
// Person.java
//
public class Person
{
public Person(String n, int f)
{
name = n;
favoriteNumber = f;
}
public String hello()
{
// formatting as hex:
// String hex = String.format("0x%08X", favoriteNumber);
return "Hello, my name is " + name +
" and my favorite number is " + favoriteNumber + ".";
}
// accessor functions
public String getName()
{
return name;
}
public int getFavoriteNumber()
{
return favoriteNumber;
}
private String name;
private int favoriteNumber;
}
//
// PersonTest.java
//
public class PersonTest
{
public static void main(String[] args)
{
// create new Person objects
Person emma = new Person("Emma", 0);
Person melodie = new Person("Melodie", 9);
Person drkessner = new Person("Dr. Kessner", 0x29a);
// call the hello() function on each
System.out.println(emma.hello());
System.out.println(melodie.hello());
System.out.println(drkessner.hello());
int sum = 0;
sum += emma.getFavoriteNumber();
sum += melodie.getFavoriteNumber();
sum += drkessner.getFavoriteNumber();
System.out.println("sum:" + sum);
}
}