0

I am reletively new to java and have been set some tasks to complete, I (think) I have completed the first two tasks which request I:

  1. Design a class Manual with the following properties:

serial number - string, - default:??????

title - string, - default: Untitled

author - string, - default: Unknown

Write a constructor and a method to print details of a Manual on the console.

.

  1. Amend your Manual class by writing the following additional methods:

methods to set and get the properties of a Manual

a method to ask the user for details of a Manual

a toString() method.

Write a simple application to test your additional methods. "


So far, I have this code:

public class Manual {

    String serialNumber, title, author;

    public static void main(String [] args){
        Manual man= new Manual();
        man.print();
    }

    public Manual(){
        set("??????", "Untitled", "Unknown");
    }

    public Manual(String serialNumber, String title, String author)
    {
        set(serialNumber, title, author);
    }

    public void set(String serialNumber, String title, String author)
    {
        this. serialNumber = serialNumber;
        this. title = title;
        this.author = author;
    }

    public void print()
    {
        System.out.println("Serial Number : " +serialNumber);
        System.out.println("Title : " +title);
        System.out.println("Author : " +author);
    }

    public void print(String heading)
    {
        System.out.println(heading);
        print();
    }

    public void ask()
    {
        serialNumber = Console.askString("Please enter the serial number: ");
        title = Console.askString("Please enter the title: ");
        author = Console.askString("Please enter the author: ");
        set(serialNumber, title, author);
    }

    public String toString()
        {
            return serialNumber +"    " +title +"    " +author +"    ";
        }
    }

Would anyone kindly be able to inform me if I have completed all areas of the first two questions correctly, and if there are any mistakes present in my code?

Thank you

05K3
  • 21
  • 6
  • Is the code above compiling without errors? You also need to add the get method for each individual variable. – Bob Ezuba Oct 29 '14 at 13:34
  • I don't know what the person that gave you this task expects. The typical `set` method is however per property, not for all at once: http://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work - speaking of getters, where are the "methods to set **and get** the properties of a Manual" ( which should probably be tested as well in your test thingy) – zapl Oct 29 '14 at 13:38
  • @zapl Thanks a lot for your feedback, how would I go about adding the get methods to my code so far? I have had a lot of issues with adding them without mistakes :( I am unsure how to do this – 05K3 Oct 29 '14 at 13:45
  • @BobEzuba Thanks for your reply, could you help me add the get methods as I have had lots of trouble before with doing this correctly :( – 05K3 Oct 29 '14 at 13:46

2 Answers2

0

The only major issue I see is that you have not implemented the Getters and Setters as was probably intended. In Java most classes have Getters/Setters for each variable that needs to be accessed, something like:

public String getTitle()
{
    return title;
}

public void setTitle(String _title)
{
    this.title = _title;
}

Also, there is nothing "wrong" with the way you have done the Print and toString functions, but I would have written up the toString to output more similar to how you have print doing it, and then calling toString from the print. Something like:

public String toString()
    {
        return "SerialNumber: " + serialNumber +"\n"
               +"Title: " + title + "\n" 
               +"Author: " + author +"\n";
    }

public void print()
{
    System.out.println(this.toString());

}

As a final note, you did not include any code to use this class, as mentioned in the last line of question 2. Hope this helps

Matthew Ertel
  • 198
  • 1
  • 10
  • Thanks for your helpful comment, I have implemented your tostring suggestion however I am still rather unsure of the get method part of your answer. Would it be possible to show me how I can correctly add this as I have been struggling on it for a long time :( – 05K3 Oct 29 '14 at 13:52
  • @05k3 I have edited my example to be exactly how you would implement it for the title variable. You would then create nearly identical functions for both serialnumber and author. Just changing the function name, the variable set, and the variable returned. – Matthew Ertel Oct 29 '14 at 13:55
  • wow thanks a lot, I noticed your set methods look different to the one present in my code, should I remove my set method and implement yours or just implement the get methods to go with my existing set method? – 05K3 Oct 29 '14 at 13:59
  • I assume this is for a class, if so your teacher probably intended for you to write out all of them as I have above. It would be impractical to have to insert all variables in a set method if you only wanted to update one of them. You could always leave yours in there as well as including all 3 sets as shown in my example, just incase. – Matthew Ertel Oct 29 '14 at 14:02
  • Here is my code now that I have implemented the get methods, is this correct? http://pastebin.com/9diRM5Mt – 05K3 Oct 29 '14 at 14:02
  • It looks correct to me, though I would still recommend including the matching set methods for each variable individually. – Matthew Ertel Oct 29 '14 at 14:04
  • Thank you, which section of my code should I remove to ensure that I only have the matching set methods present? – 05K3 Oct 29 '14 at 14:10
  • You don't need to remove anything, just add a set method for each variable, like the one I have above. Also, if this helped you out please select it as your chosen answer. :) – Matthew Ertel Oct 29 '14 at 14:21
0

This is how I would implement the get() method for all three variables.

public String getSerialNumber(){
    return serialNumber;
}

public String getTitle(){
    return title;
}

public String getAuthor(){
    return author;
}
Bob Ezuba
  • 510
  • 1
  • 5
  • 22