I'm trying to write a personclass and a personTester in Java - java

So far for my personClass, I have the following:
package personclass;
public class Personclass {
private static boolean Personclass;
private int PersonCount;
public int getPersonCount()
{
return PersonCount;
}
private String FirstName;
private String LastName;
private int Age;
private Double Height;
private String Gender;
public Personclass(String foreName, String surName, int age, Double height, String gender)
{
FirstName = foreName;
LastName = surName;
Age = age;
Height = height;
Gender = gender;
}
private String getFirstName()
{
return FirstName;
}
private void setFirstName(String foreName)
{
this.FirstName = foreName;
}
private String getLastName()
{
return LastName;
}
private void setLastName(String surName)
{
this.LastName = surName;
}
private int getAge()
{
return Age;
}
private void setAge(int age)
{
this.Age = age;
}
private Double getHeight()
{
return Height;
}
private void setHeight(Double height)
{
this.Height = height;
}
private String getGender()
{
return Gender;
}
private void setGender(String gender)
{
this.Gender = gender;
}
/**
*
* #param FirstName
* #param LastName
* #param Age
* #param Height
* #param Gender
*/
public Personclass(String FirstName, String LastName, int Age, double Height, String Gender)
{
this.FirstName = FirstName;
this.LastName = LastName;
this.Age = Age;
this.Height = Height;
this.Gender = Gender;
++PersonCount;
}
/**
*
* #return
*/
#Override
public String toString()
{
return "Person[forename=" + getFirstName() + ", surname=" + getLastName() + ", age=" + getAge() + ", height=" + getHeight() +"m" + ", gender=" + getGender() +"]";
}
public String format()
{
return String.format("%10s %10s %10d %10.2f %10s", getFirstName() , getLastName() , getAge() , getHeight() , getGender());
}
public static void main(String[] args)
{
}
}
And for my personTester, I have the following code:
package personclass;
public class PersonTester
{
public static void main(String[] args)
{
Person person1 = new Person("Joe","Smith",25,1.57,"Male");
Person person2 = new Person("Sain","Davies",18,1.73,"Female");
Person person3 = new Person("John","White",22,1.60,"Male");
Person person4 = new Person("Martin","Taylor",26, 1.54,"Male");
Person person5 = new Person("Jessica","Clarke",19,1.70,"Female");
System.out.println(person1.toString());
System.out.println(person2.toString());
System.out.println(person3.toString());
System.out.println(person4.toString());
System.out.println(person5.toString());
}
}
The thing that I'm having trouble with is not being about to print anything out when I try and run the personTester. How would I go about trying to print the five different people out?

You need to make the objects in main() in class PersonTester type Personclass. That should enable you to print the information about the people, through the toString() method in that class.

There can be two possible solutions.
You should make sure that you are running the PersonTester class, but not Personclass. i.e., You should be using java PersonTester command if you are running from command line.
The name of your class is Personclass but in your PersonTester class, you are referring to some Person class. You should change the Personclass to something like the following.
package personclass;
public class PersonTester{
public static void main(String[] args) {
Personclass person1 = new Personclass ("Joe","Smith",25,1.57,"Male");
Personclass person2 = new Personclass ("Sain","Davies",18,1.73,"Female");
Personclass person3 = new Personclass ("John","White",22,1.60,"Male");
Personclass person4 = new Personclass ("Martin","Taylor",26, 1.54,"Male");
Personclass person5 = new Personclass ("Jessica","Clarke",19,1.70,"Female");
System.out.println(person1.toString());
System.out.println(person2.toString());
System.out.println(person3.toString());
System.out.println(person4.toString());
System.out.println(person5.toString());
}
}

Related

I am trying to serialize multiple objects and i could quite get pass the error of trying to add values to may ArrayList (Book)

in the line of books.add it says incompatible types: string cannot be converted to Author
I tried converting it all to string but it still didn't work. My problem is how do i make the string be converted to author. The expected output of this is to simply create an arraylist of 5 books with the class of authors, books, and publishing dat. Under the Book class, i am trying to add the arraylist but i cannot seem to get pass the problem of the string to be converting to the object author.
public class Book {
private String bookTitle;
private Author name;
private PubDate date;
public Book() {
}
public Book(String bookTitle, Author name, PubDate date) {
this.bookTitle = bookTitle;
this.name = name;
this.date = date;
}
public String getBookTitle() {
return bookTitle;
}
public Author getName() {
return name;
}
public PubDate getDate() {
return date;
}
public void setBookTitle(String bookTitle) {
this.bookTitle = bookTitle;
}
public void setName(Author name) {
this.name = name;
}
public void setDate(PubDate date) {
this.date = date;
}
#Override
public String toString() {
return "Book{" + "bookTitle=" + bookTitle + ", name=" + name + ", date=" + date + '}';
}
}
public class Author {
private String firstname;
private String lastname;
private String title;
public Author() {
}
public Author(String firstname, String lastname, String title) {
this.firstname = firstname;
this.lastname = lastname;
this.title = title;
}
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
public String getTitle() {
return title;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public void setTitle(String title) {
this.title = title;
}
#Override
public String toString() {
return "Author{" + "firstname=" + firstname + ", lastname=" + lastname + ", title=" + title + '}';
}
}
public class Serialization {
public static void main(String[] args) {
ArrayList<Book> books = new ArrayList<Book>();
ArrayList<Author> authors = new ArrayList<Author>();
ArrayList<PubDate> pubs = new ArrayList<PubDate>();
books.add(new Book("Book Title", "Author Name", "Publishing Date"));
}
}

How to create objects for parameterized constructors in Java, when we have two classes with the same attributes?

I have class Student, that has first name, last name and age, and a method to print the name and the age of the student.
public class Student {
private String firstName;
private String LastName;
private int age;
}
Student() {}
public Student(String firstName, String lastName, int age){
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void printInfoStudent(){
System.out.println("Student: " + firstName + " " + lastName + ", " + age);
}
And I have a second class Professor, that has first name, last name, and university. And I have a method to print the info about the professor.
public class Professor {
private Student firstName;
private Student lastName;
private String uni;
}
Professor() {
}
public Professor(Student firstName, Student lastName, String uni) {
this.firstName = firstName;
this.lastName = lastName;
this.uni = uni;
}
public Student getFirstName() {
return firstName;
}
public void setFirstName(Student firstName) {
this.firstName = firstName;
}
public Student getLastName() {
return lastName;
}
public void setLastName(Student lastName) {
this.lastName = lastName;
}
public String getUni() {
return uni;
}
public void setUni(String uni) {
this.uni = uni;
}
public void printInfo(){
System.out.println("Professor: " + firstName + " " + lastName + ", uni: " + university);
}
And in the main class I create two objects.
Student s = new Student("John", "John", 24);
Professor p = new Professor("Johnny", "Johnny", "Oxford");
printInfo.p();
printInfoStudent.s();
And it's showing me an error: String cannot be converted to Student, can someone explain why is that, and how should I fix this?
You've created the objects properly but are not calling their functions properly.
Student s = new Student("John", "John", 24);
Professor p = new Professor("Johnny", "Johnny", "Oxford");
p.printInfo()
s.printInfoStudent();
You need to name the instance first then specify the method inside to call after the dot.

Best way to handle mutable fields while making a class Immutable

I want to make Footballer class immutable, it has Address field which is mutable, so what is the best way to make it immutable,
Typically for HomeAddress and Area,I can use
Clone
Serialization
Copy Constructor
Since clone is broken,How to chose between copy constructor or Serialization.
package com.javaeight;
import java.util.ArrayList;
import java.util.List;
public class ImmutableDemo {
public static void main(String[] args) {
String name = "ronaldo";
int age = 27;
List<String> clubs = new ArrayList<>();
clubs.add("MU");
clubs.add("BC");
Area area = new Area(22, 11);
HomeAddress address = new HomeAddress("Bangalore", "India", area);
Footballer footballer = new Footballer(name, age, clubs, address);
}
}
final class Footballer {
private final String name;
private final int age;
private final List<String> clubs;
private final HomeAddress address;
public Footballer(String name, int age, List<String> clubs, HomeAddress homeAddress) {
this.name = name;
this.age = age;
this.clubs = new ArrayList<>(clubs);
this.address = new HomeAddress(homeAddress.getCity(), homeAddress.getCountry(), homeAddress.getArea());
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public List<String> getClubs() {
return new ArrayList<>(clubs);
}
#Override
public String toString() {
return "Footballer{" +
"name='" + name + '\'' +
", age=" + age +
", clubs=" + clubs +
'}';
}
}
class HomeAddress {
private String city;
private String country;
private Area area;
public HomeAddress(String city, String country, Area area) {
this.city = city;
this.country = country;
this.area = area;
}
public String getCity() {
return city;
}
public String getCountry() {
return country;
}
public Area getArea() {
return area;
}
}
class Area {
private int Street;
private int buildingNumber;
public Area(int street, int buildingNumber) {
Street = street;
this.buildingNumber = buildingNumber;
}
public int getStreet() {
return Street;
}
public int getBuildingNumber() {
return buildingNumber;
}
}

Overloaded constructors in a class

I'm doing a project with overloaded constructors in a class and I'm a little stuck, below is what I'm supposed to be doing with the overloaded constructors:
"One that allows first, middle, and last names to be passed as Strings with an int for age
One that accepts a Name object reference, and an age as an int
Make a new Name inside Person, copying the references for the parts of the name."
I'm not quite sure what to do with my code, here is what I got:
public class Person {
int age;
Name aPersonHasAName;
Name newPerson = new Name();
public Person(String firstName, String middleName, String lastName, int age) {
newPerson.firstName = firstName;
newPerson.middleName = middleName;
newPerson.lastName = lastName;
}
public Person(Name aPersonHasAName, int age) {
}
public void details() {
System.out.println(aPersonHasAName + " age: " + age);
}
}
I'm just lost as to what I'm supposed to be typing. I believe I've done the first overloaded constructor, but I am new to this.
So what should I be doing to make this work with overloaded constructors?
I think having the code from the other two classes might help.
Here is PersonTester:
public class PersonTester {
public static void main(String[] args) {
Person person1 = new Person("a1", "b1", "c1", 11);
Person person2 = new Person(new Name("a2", "b2", "c2"), 22);
Person person3 = new Person(new Name("a3", "c3"), 33);
Person person4 = new Person(new Name("a4"), 44);
Person person5 = new Person(new Name(), 55);
System.out.println(person1.details());
System.out.println(person2.details());
System.out.println(person3.details());
System.out.println(person4.details());
System.out.println(person5.details());
}
}
Then here is the Name class:
public class Name {
String firstName;
String middleName;
String lastName;
public Name(String firstName, String middleName, String lastName) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
}
public Name(String firstName, String lastName) {
this(firstName, "", lastName);
}
public Name(String firstName) {
this(firstName, "", "");
}
public Name() {
this("", "", "");
}
public String getLastName() {
return lastName;
}
public String getMiddleName() {
return middleName;
}
public String getFirstName() {
return firstName;
}
public String getFullName(String nameString) {
StringBuilder build = new StringBuilder();
build.append(nameString);
build.deleteCharAt(nameString.length() - 1);
build.insert(0, build.hashCode());
return build.toString();
}
}
The problem I am having now is the error message in PersonTester which is: The method println(boolean) in the type PrintStream is not applicable for the arguments (void)
I just need to know what in which class needs to be fixed to make it work.
I am very new to Java and object oriented programming.
So far so good. But eventually you'll reach a point where you duplicate a fair bit of code.
The constructor
public Person(String firstName, String middleName, String lastName, int age) {
is the most comprehensive one in the sense that it takes in all the possible data.
With the other constructors, say one that takes a last name and an age, you can use delegating constructors:
public Person(String lastName, int age) {
this(null, null, lastName, age); /*calls the other constructor*/
}
If you can't make such an assumption then you'll need to split up the name string by hand.
Updating your code:
public Person(String firstName, String middleName, String lastName, int age) {
newPerson.firstName = firstName;
newPerson.middleName =
newPerson.lastName = lastName;
this.age = age; //<---- was missing in your code
}
And your second contructor may look like this:
public Person(Name aPersonHasAName, int age) {
this.newPerson = aPersonHasAName;
this.age = age;
}
These contructors are implemented as you needed.
Notice that you already done your overloading, if you got multiple constructors with not the same titles you contructors overloading
public class Person {
int age;
Name aPersonHasAName;
public Person(String firstName, String middleName, String lastName, int age) {
aPersonHasAName = new Name();
aPersonHasAName.firstName = firstName;
aPersonHasAName.middleName = middleName;
aPersonHasAName.lastName = lastName;
this.age = age;
}
public Person(Name aPersonHasAName, int age) {
this.aPersonHasAName = aPersonHasAName;
this.age = age;
}
public void details() {
System.out.println(aPersonHasAName + " age: " + age);
}
}
I guess it also depends on if Name has a constructor for firstName, middleName, lastName

Work with methods, setters and getters in java

I have this class:
public class Person
{
/**
*
*/
private static final long serialVersionUID = 1L;
private String firstName = "Vasya";
private String lastName = "Pupkin";
private Integer age = 58;
private Integer phone = 2;
#Override
public String toString()
{
return "Person [firstName=" + firstName + ", lastName=" + lastName
+ ", age=" + age + "]";
}
public void setName(String name)
{
firstName = name;
}
public void setLastName(String lName)
{
lastName = lName;
}
public void setAge(Integer personAge)
{
age = personAge;
}
public void setPhone(Integer personPhone)
{
phone = personPhone;
}
public String getName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public Integer getAge()
{
return age;
}
public Integer getPhone()
{
return phone;
}
public void Init()
{
this.setName("");
this.setLastName("");
this.setPhone(0);
this.setAge(0);
}
}
I create an variable: Person somePerson, then I call method setName from that variable somePerson:
somePerson.setName("");
but it raises an error.
Based on the provided code, the following should work:
Person somePerson = new Person();
somePerson.setName("");
If it doesn't, then something else is going on.

Categories