1

This is my first programming assignment, so I'm obviously a novice. The criteria of my assignment is to create an assignment class that takes in 4 variables. Then I am to make a program that accepts user's input for those variable, and then creates an objects, using previously ascribed getter methods.

AssignmentCreator Class
This class will use the Assignment class to create an assignment.
First, ask the user to enter the title, category, max points, and due date for the assignment.
Create an object of the Assignment class using the argument constructor and the user’s input.
Using the getter methods, display the assignment’s information, as shown below. The output that should be produced by this class is shown below in the sample execution section.

My initial Assignment class file:

/**
   Describes an assignment's title, due date, total points value, and category
*/

public class Assignment 
{
   private String title;     //Title of assignment
   private String dueDate;   //Due date of assignment
   private double maxPoints; //Max points of assignment
   private String category;  //Category of assignment

   /**
      Initialize instance variables for assignment project (no argument-constructor)
   */ 
   public Assignment()  
   {
      title = "Assignment 1";
      dueDate = "01/01/2019";
      maxPoints = 10.0;
      category = "Programming Assignments";
   }

   /** 
      Initialize instance variables for the assignment project (argument constructor)
      @param t title of assignment
      @param d due date of assignment
      @param m max points for the assignment
      @param c category of assignment
   */ 
   public Assignment(String t, String d, double m,String c)   
   {
      title = t; 
      dueDate = d;
      maxPoints = m;
      category = c;
   }

   /**
      Sets the value of title
      @param t title of assignment
   */
   public void setTitle(String t)
   {
      title = t; 
   }

   /**
      Sets the value of dueDate
      @param d due date of assignment
   */
   public void setDueDate(String d)
   {
      dueDate = d;
   }

   /**
      Sets value of maxPoints
      @param m max points of assignment
   */
   public void setMaxPoints(double m)
   {
      maxPoints = m;
   }

   /**
      Sets the value of category
      @param c category of assignment
   */
   public void setCategory(String c)
   {
      category = c;
   }

   /**
      Returns the value of title
      @return title of assingment
   */
   public String getTitle()
   {
      return title;
   }

   /**
      Returns the value of dueDate
      @return due date of assignment
   */
   public String getDueDate()
   {
      return dueDate;
   }

   /**
      Returns the value of maxPoints
      @return max points of assignment
   */
   public double getMaxPoints()
   {
      return maxPoints;
   }

   /**
      Returns the value of category
      @return category of assingmen
   */
   public String getCategory()
   {
      return category;
   }
}

This is where I am currently stuck:

/**  
   imports scanner used for user import  
*/  
import java.util.Scanner;   

public class AssignmentCreator  
{  
   public static void main(String[] args)   

   {  
      Scanner S = new Scanner(System.in);

      System.out.print("Create Assignment \n=================\n\n");

      System.out.print("Enter title: ");
      String t = S.nextLine();

      System.out.print("Enter category: ");
      String c = S.nextLine();

      System.out.print("Enter max points: ");
      double m = S.nextDouble();

      System.out.print("Enter due date (MM/DD/YYYY): ");
      String d = S.nextLine();

      Assignment create = new Assignment(t, c, m, d);
      System.out.print("Title of the assignment is: " + create.getTitle());
      System.out.print("category of the assignment is: " + create.getCategory());
      System.out.print("max points of the assignment is: " + create.getMaxPoints());
      System.out.print("due date of the assignment is: " + create.getDueDate());

Now, I know that this correctly asks for input and stores it to 4 variables, but I'm confused as to how to do as the instructions above say.

I also understand how "System.out.println("| Category | " + c);" etc. would create and show what was stored, but thats not exactly what I was instructed to do.

CS_noob
  • 512
  • 1
  • 4
  • 16
  • 1
    change this also `String m = S.nextLine()` to `Double m=S.nextDouble()` since you should pass `Double` into constructor on 3rd param. `Assignment create = new Assignment(t, d, m, c)` – Traian GEICU Jan 28 '19 at 05:43
  • @TraianGEICU Ah, thanks. Why is this one with a capital "D"? –  Jan 28 '19 at 05:48
  • 1
    `double` is a primitive. `Double` is an object wrapper for `double`. `double m=S.nextDouble()` is also fine. Basically they hold both double value, but on object you have also methods. Primitive do not have methods. (other example `int`_primitive and `Integer`_wrapper class). https://docs.oracle.com/javase/10/docs/api/java/lang/Double.html. May look further also at boxing and unboxig. – Traian GEICU Jan 28 '19 at 05:51
  • 2
    `Double obj = null;` works, but `double obj = null;` does not. Therefore, `Double` and `double` can not hold the same information. – Johannes Kuhn Jan 28 '19 at 06:14
  • 2
    @JohannesKuhn was "basically" with out enter into edge details. Better say `double` is included in `Double` as information that could be stored. Good for bringing additional knowledge. – Traian GEICU Jan 28 '19 at 06:30

1 Answers1

0

Using the getter methods, display the assignment’s information, as shown below.

I don't know what the desired format of the output is, but I would assume that you're being asked to print the field values of the Assignment instance by doing something like

System.out.print("Title of the assignment is: " + create.getTitle());
namuny
  • 139
  • 4
  • That seems like the right answer, but it prints the getTitle value assigned in my original Assignment class. How do I getTitle for what the user inputs? –  Jan 28 '19 at 05:31
  • 1
    You'll need to instantiate the Assignment class using your constructor. Pass the user input values into the constructor of Assignment class and you'll have an object with the correct values. – namuny Jan 28 '19 at 05:33
  • Ok, and would that constructor be applied with the new operator? As in: "Assignment create = new Assignment(String t, String c, double m, String d)? –  Jan 28 '19 at 05:36
  • 1
    Almost. You want to pass the input variables to the constructor, right? Since you already have variables for t, c, m, d, you don't need to declare their types again. Simple use the variable names and pass them to the constructor. – namuny Jan 28 '19 at 05:38
  • Thank you. That helped, but I've ran into something else. I've edited my code above to implement your suggested change I'm prompted with what I want the user to input, but it doesnt take in the last line (due date) and it spits out mixed results like so: Enter title: test Enter category: test2 Enter max points: 1.0 Enter due date (MM/DD/YYYY): Title of the assignment is: test category of the assignment is: max points of the assignment is: 1.0 due date of the assignment is: test2 –  Jan 28 '19 at 06:03
  • @namumy Sorry, it wouldnt let me format that comment better :( –  Jan 28 '19 at 06:03
  • 1
    Take a look at your constructor: public Assignment(String t, String d, double m,String c) And see what order you're providing the variables in when you create a new instance of Assignment. You'll see that the input order is not what you think :) – namuny Jan 28 '19 at 06:05
  • @namumy right, I assigned 4 variables to the 4 inputs, and then I passed those 4 variables to the constructor without declaring their types? –  Jan 28 '19 at 06:08
  • 1
    Not quite what I mean. Take a look at the order of arguments in your constructor definition (inside the Assignment.java file). It takes title, duedate, maxpoints, category in this SPECIFIC order. Now look at how you're instantiating the Assignment object. You're providing the arguments in the order of title, category, maxpoints, duedate. Do you see what's wrong here? – namuny Jan 28 '19 at 06:09
  • @namumy OOOOOOH, I didnt realize you meant that constructor. Now I understand, and now the program takes the correct order. But it still does not accept the 4th input for due date - it stops right after the 3rd input. –  Jan 28 '19 at 06:14
  • You might want to take a look at https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo This explains it better than I ever could :) – namuny Jan 28 '19 at 06:19