0

Just started studying java and have an exercise to complete.

I have the following code:

package exercise1;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Singer {

//Class Parameters
int id;
String name;
String address;
Date dob;
int nAlbums;

//Overloaded Constructors
public Singer()
{
    
}

public Singer(int id)
{
this.id = id;
}

public Singer(int id, String name)
{
this.id = id;
this.name = name;
}

public Singer(int id, String name, String address)
{
this.id = id;
this.name = name;
this.address = address;
}

public Singer(int id, String name, String address, Date dob)
{
this.id = id;
this.name = name;
this.address = address;
this.dob = dob;
}

public Singer(int id, String name, String address, Date dob, int nAlbums)
{
this.id = id;
this.name = name;
this.address = address;
this.dob = dob;
this.nAlbums = nAlbums;
}

//All Setters
public void setId (int id)
{

    this.id = id;
    
}

public void setName (String name)
{

    this.name = name;
    
}

public void setAddress (String address)
{

    this.address = address;
    
}

public void setDOB (Date dob)
{

    this.dob = dob;
    
}

public void setNAlbums (int nAlbums)
{

    this.nAlbums = nAlbums;
    
}

public void setSinger (int id, String name, String address, Date dob, int nAlbums)
{
    this.id = id;
    this.name = name;
    this.address = address;
    this.dob = dob;
    this.nAlbums = nAlbums;
    
}

//All Getters

public int ID ()
{
    return id;
}

public String Name ()
{
    return name;
}

public String Address ()
{
    return address;
}

public Date DOB ()
{
    return dob;
}

public int NAlbums ()
{
    return nAlbums;
}

public String Display()
{
    return "\nID: " + id + "\nName: " + name + "\nAddress: " + address + "\nBirthday: " +dob+ "\nNumber of Albums: "+nAlbums;
}
public static void main(String[] args) {
    
    String pattern = "yyyy-MM-dd";
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    
    Singer singer1 = new Singer();

    System.out.println("\nID (singer1): "+ singer1.id);

    System.out.println("\nName (singer1) :"+ singer1.name);

    System.out.println("\nAddress (singer1) :"+ singer1.address);
    
    System.out.println("\nBirthday (singer1) :"+ singer1.dob);
    
    System.out.println("\nNumber of Albums (singer1) :"+ singer1.nAlbums);

        singer1.setSinger(1, "Davy Jones", "12 Main Street", sdf.parse("1947-01-08"), 27);
            
    System.out.println("\nsinger1 Details: "+ singer1.Display());

        
}

}

So two errors are happening:

  1. it is asking me to throw a parseexception;
  2. Even when I accept the correction I receive the dates through the following format:
singer1 Details: 
ID: 1
Name: Davy Jones
Address: 12 Main Street
Birthday: Wed Jan 08 00:01:00 EST 1947
Number of Albums: 27

Using Eclipse

Thanks for your help in advance!

EDIT

Thanks for your help, here's the final code:

package exercise1;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Singer {

//Class Parameters
int id;
String name;
String address;
LocalDate dob;
int nAlbums;

//Overloaded Constructors
public Singer()
{
    
}

public Singer(int id)
{
this.id = id;
}

public Singer(int id, String name)
{
this.id = id;
this.name = name;
}

public Singer(int id, String name, String address)
{
this.id = id;
this.name = name;
this.address = address;
}

public Singer(int id, String name, String address, LocalDate dob)
{
this.id = id;
this.name = name;
this.address = address;
this.dob = dob;
}

public Singer(int id, String name, String address, LocalDate dob, int nAlbums)
{
this.id = id;
this.name = name;
this.address = address;
this.dob = dob;
this.nAlbums = nAlbums;
}

//All Setters
public void setId (int id)
{

    this.id = id;
    
}

public void setName (String name)
{

    this.name = name;
    
}

public void setAddress (String address)
{

    this.address = address;
    
}

public void setDOB (LocalDate dob)
{

    this.dob = dob;
    
}

public void setNAlbums (int nAlbums)
{

    this.nAlbums = nAlbums;
    
}

public void setSinger (int id, String name, String address, LocalDate dob, int nAlbums)
{
    this.id = id;
    this.name = name;
    this.address = address;
    this.dob = dob;
    this.nAlbums = nAlbums;
    
}

//All Getters

public int ID ()
{
    return id;
}

public String Name ()
{
    return name;
}

public String Address ()
{
    return address;
}

public LocalDate DOB ()
{
    return dob;
}

public int NAlbums ()
{
    return nAlbums;
}

public String Display()
{
    return "\nID: " + id + "\nName: " + name + "\nAddress: " + address + "\nBirthday: " +dob+ "\nNumber of Albums: "+nAlbums;
}
public static void main(String[] args) {
    
    Singer singer1 = new Singer();
    
    System.out.println("\nID (singer1): "+ singer1.id);

    System.out.println("\nName (singer1) :"+ singer1.name);

    System.out.println("\nAddress (singer1) :"+ singer1.address);
    
    System.out.println("\nBirthday (singer1) :"+ singer1.dob);
    
    System.out.println("\nNumber of Albums (singer1) :"+ singer1.nAlbums);

    singer1.id = 1;
    
    singer1.name = "Davy Jones";
    
    singer1.address = "12 Main Street";
    
    String string = "January 08, 1947";
    
    DateTimeFormatter pattern = DateTimeFormatter.ofPattern("MMMM dd, yyyy", Locale.ENGLISH);
    
    LocalDate date = LocalDate.parse(string, pattern);
    
    singer1.dob = date;
    
    singer1.nAlbums = 27;
    
    System.out.println("\nAll singer properties set\n");
    
    System.out.println("\nsinger1 Details: "+ singer1.Display());
    

}

}

  • 1
    The question needs clarifications: You got a java Error asking you to throw a ParseException or catch one? Either way, why would we want to do either here? Oracle doc says various constructors and methods for SimpleDateFormat can throw NullPointerException or IllegalArgumentException I see no ParseException anywhere. – Omar Abdel Bari Sep 18 '20 at 01:46
  • **#1** See the link up top that explains what a *checked* exception is. --- **#2:** That is the default format when printing a `Date` value, as **documented** in the javadoc of the [`toString()`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#toString--) method of `Date`. If you want a different format, use a `SimpleDateFormat` and call the `format()` method. – Andreas Sep 18 '20 at 03:31
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). This also will not require declaring `thorws ParseException`and it will display the date back as `1947-01-08` – Ole V.V. Sep 18 '20 at 04:17
  • 1
    Thanks a lot for the responses. As per @OleV.V. suggestion I have pivoted into the LocalDate. It definitely felt smoother. Thanks again! – freddhesse Sep 21 '20 at 02:29

0 Answers0