14 Replies - 3794 Views - Last Post: 01 November 2014 - 12:41 AM Rate Topic: -----

#1 MBStevens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-October 14

prompt a user to enter three tasks and their priorities in a driver

Posted 31 October 2014 - 10:15 PM

My full instructions are to Design a Java interface called Priority that includes two methods and two constants. The methods are
setPriority and getPriority. The constants are MIN_PRIORITY with value 1 and MAX_PRIORITY with value 6. The
interface should define a way to establish numeric priority among a set of objects.
Design and implement a class called Task that represents a task (such as on a to-do list) that implements the
Priority interface. Assume that Task has two attributes (instance data): name and priority. Define the Task
constructor to accept and initialize the name of the task. Each newly created Task should have priority equal to
MIN_PRIORITY (the constructor should initialize priority to MIN_PRIORITY). Priorities can be changed using
setPriority method.
Create a driver class TaskDriver that should do the following: prompt the user to enter three tasks and their
priorities, and then print the tasks in the order of decreasing priorities. If the user enters an invalid priority that is
less than MIN_PRIORITY or greater than MAX_PRIORITY, then the user should be asked to enter priority again
until a valid priority value is entered.

So far I have:

public interface Priority
{
 public void setPriority(int level);
 public int getPriority();
 
  int MIN_PRIORITY = 1;
  int MAX_PRIORITY = 6;
}


public class Task implements Priority {

   private int priority;  // the priority held by the class

	public void setPriority(int MIN_PRIORITY){
             priority = MIN_PRIORITY;     // save priority in instance variable
	}
        // getPriority should not have a parameter		
	public int getPriority(){
		return priority;    // return saved priority
	}
}	




import java.util.Scanner;

public class TaskDriver
{
 public static void main (String [] args)
 {
  Scanner scan = new Scanner (System.in);
 
  Priority priority1 = new priority ("Please enter the name of the 1st task: ");
  S = scan.nextLine();
  
 }
}


Assuming the interface and class are right, I'm having problems with the driver class and getting errors such as
TaskDriver.java:9: error: cannot find symbol
  Priority priority1 = new priority ("Please enter the name of the 1st task: ");
                           ^
  symbol:   class priority
  location: class TaskDriver
TaskDriver.java:10: error: cannot find symbol
  S = scan.nextLine();
  ^
  symbol:   variable S
  location: class TaskDriver
2 errors


Is This A Good Question/Topic? 0
  • +

Replies To: prompt a user to enter three tasks and their priorities in a driver

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,396
  • Joined: 27-December 08

Re: prompt a user to enter three tasks and their priorities in a driver

Posted 31 October 2014 - 10:20 PM

There are a few misconceptions I think we should clear up. First, interfaces are not instantiable. Second, the constructor name must match the class name, in spelling and case. So this line is illegal for those two reasons (amongst others): Priority priority1 = new priority ("Please enter the name of the 1st task: ");.

Here: public void setPriority(int MIN_PRIORITY){, the nomenclature is confusing. Task implements Priority, so task inherits the constant MIN_PRIORITY. Don't use the same name for the parameter as for the constant. If you want a constraint:
public void setPriority(int newPriority){
   if(newPriority >= MIN_PRIORITY && newPriorty <= MAX_PRIORITY){
     //update this.priority
   }
}



Here: S = scan.nextLine();, you never declared S.
Was This Post Helpful? 1
  • +
  • -

#3 MBStevens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-October 14

Re: prompt a user to enter three tasks and their priorities in a driver

Posted 31 October 2014 - 10:31 PM

View Postmacosxnerd101, on 31 October 2014 - 10:20 PM, said:

There are a few misconceptions I think we should clear up. First, interfaces are not instantiable. Second, the constructor name must match the class name, in spelling and case. So this line is illegal for those two reasons (amongst others): Priority priority1 = new priority ("Please enter the name of the 1st task: ");.

Here: public void setPriority(int MIN_PRIORITY){, the nomenclature is confusing. Task implements Priority, so task inherits the constant MIN_PRIORITY. Don't use the same name for the parameter as for the constant. If you want a constraint:
public void setPriority(int newPriority){
   if(newPriority >= MIN_PRIORITY && newPriorty <= MAX_PRIORITY){
     //update this.priority
   }
}



Here: S = scan.nextLine();, you never declared S.



I'm a little confused on what to do for the Priority priority1 = new priority ("Please enter the name of the 1st task: "); part. Also, when I did that
public class Task implements Priority 
{
 private int priority;

 public void setPriority(int newPriority)
 {
  if(newPriority >= MIN_PRIORITY && newPriorty <= MAX_PRIORITY){
 }
	
  public int getPriority()
  {
   return priority;   
  }
 }
}


I get error
Task.java:10: error: illegal start of expression
  public int getPriority()
  ^
Task.java:10: error: ';' expected
  public int getPriority()
                        ^
2 errors


Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,396
  • Joined: 27-December 08

Re: prompt a user to enter three tasks and their priorities in a driver

Posted 31 October 2014 - 10:33 PM

1) You would instantiate Task, not Priority. Remember, a Task has Priority. The Priority itself is not an object.

2) Check your braces. You are missing a closing brace for the setPriority() method. Make sure to better indent your code and you should see this immediately.
public void setPriority(int newPriority)
06	 {
07	  if(newPriority >= MIN_PRIORITY && newPriorty <= MAX_PRIORITY){
08	 }


Was This Post Helpful? 1
  • +
  • -

#5 MBStevens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-October 14

Re: prompt a user to enter three tasks and their priorities in a driver

Posted 31 October 2014 - 10:45 PM

View Postmacosxnerd101, on 31 October 2014 - 10:33 PM, said:

1) You would instantiate Task, not Priority. Remember, a Task has Priority. The Priority itself is not an object.

2) Check your braces. You are missing a closing brace for the setPriority() method. Make sure to better indent your code and you should see this immediately.
public void setPriority(int newPriority)
06	 {
07	  if(newPriority >= MIN_PRIORITY && newPriorty <= MAX_PRIORITY){
08	 }



Sorry for being such a noob at all of this, It's my first class ever learning how to code.

so for 1) I have this now
import java.util.Scanner;

public class TaskDriver
{
 public static void main (String [] args)
 {
  int s;
  
  Scanner scan = new Scanner (System.in);
 
  Task task1 = new task ("Please enter the name of the 1st task: ");
  s = scan.nextLine();
  
 }
}



as for 2) I can't figure out which braces are messing things up, I think I just made it worse.
public class Task implements Priority 
{
 private int priority;
 
  public void setPriority(int newPriority){

   if(newPriority >= MIN_PRIORITY && newPriorty <= MAX_PRIORITY){
  	}
  }
   public int getPriority(){
  
   return priority;   
  
 }
}


Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,396
  • Joined: 27-December 08

Re: prompt a user to enter three tasks and their priorities in a driver

Posted 31 October 2014 - 10:48 PM

Quote

so for 1) I have this now

Note the nextLine() method returns a String, not an int. You should get a compiler error telling you this.

I formatted your code a little bit. It's easier to see where things line up. It looks like it should compile to me, but test it of course.
public class Task implements Priority 
{
 private int priority;
 
  public void setPriority(int newPriority){

     if(newPriority >= MIN_PRIORITY && newPriorty <= MAX_PRIORITY){

     }
  }
  
  public int getPriority(){
  
   return priority;   
  
 }
}


Was This Post Helpful? 1
  • +
  • -

#7 MBStevens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-October 14

Re: prompt a user to enter three tasks and their priorities in a driver

Posted 31 October 2014 - 10:53 PM

Getting error
Task.java:7: error: cannot find symbol
     if(newPriority >= MIN_PRIORITY && newPriorty <= MAX_PRIORITY){
                                       ^
  symbol:   variable newPriorty
  location: class Task
1 error


I can't run TaskDriver with the error on Task so I'm not sure what error(s) might be there.

This post has been edited by macosxnerd101: 31 October 2014 - 10:54 PM
Reason for edit:: No need to quote the post right above

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,396
  • Joined: 27-December 08

Re: prompt a user to enter three tasks and their priorities in a driver

Posted 31 October 2014 - 10:55 PM

Check your spelling here: newPriorty.
Was This Post Helpful? 1
  • +
  • -

#9 MBStevens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-October 14

Re: prompt a user to enter three tasks and their priorities in a driver

Posted 31 October 2014 - 10:59 PM

View Postmacosxnerd101, on 31 October 2014 - 10:55 PM, said:

Check your spelling here: newPriorty.


Ha, can't believe I didn't see that. So now Task compiles. For Task Driver I changed it to
import java.util.Scanner;

public class TaskDriver
{
 public static void main (String [] args)
 {
  int s;
  
  Scanner scan = new Scanner (System.in);
 
  Task task1 = new Task ("Please enter the name of the 1st task: ");
  s = scan.nextInt();
  
 }
}


I'm getting error
 TaskDriver.java:11: error: constructor Task in class Task cannot be applied to given types;
  Task task1 = new Task ("Please enter the name of the 1st task: ");
               ^
  required: no arguments
  found: String
  reason: actual and formal argument lists differ in length
1 error



Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,396
  • Joined: 27-December 08

Re: prompt a user to enter three tasks and their priorities in a driver

Posted 31 October 2014 - 11:02 PM

The error message is pretty self-explanatory. If you're working with interfaces, I'm assuming you've done the basics of OOP then, right? So you should be familiar with the basics of constructors and methods.

Hint #1: Look at the Task class. Do you see a constructor accepting a String?

Hint #2: A Task shouldn't be responsible for getting its own name from the user.
Was This Post Helpful? 1
  • +
  • -

#11 MBStevens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-October 14

Re: prompt a user to enter three tasks and their priorities in a driver

Posted 31 October 2014 - 11:16 PM

View Postmacosxnerd101, on 31 October 2014 - 11:02 PM, said:

The error message is pretty self-explanatory. If you're working with interfaces, I'm assuming you've done the basics of OOP then, right? So you should be familiar with the basics of constructors and methods.

Hint #1: Look at the Task class. Do you see a constructor accepting a String?

Hint #2: A Task shouldn't be responsible for getting its own name from the user.


I'm still learning the basics, this is the first program I've done with interfaces. I came up with this
public class Task implements Priority 
{
 private int priority;
 
 public Task (String Task, int priority)
 {
 this.Task = Task;
 this.priority = priority;
 }
 
  public void setPriority(int newPriority){

     if(newPriority >= MIN_PRIORITY && newPriority <= MAX_PRIORITY){

     }
  }
  
  public int getPriority(){
  
   return priority;   
  
 }
}



but I'm getting error
Task.java:7: error: cannot find symbol
 this.Task = Task;
     ^
  symbol: variable Task
1 error
I'm assuming its because its called "Task" but I thought that's what I had to call it because that's what it's called in TaskDriver.

The main point of this program is to allow users to input a string, and set the priority 1-6, and then the program puts the list in order.

like this:

Please enter the name of the 1st task: Eat lunch
What is its priority? 9
Priority should be between 1 and 6
What is its priority? 0
Priority should be between 1 and 6
What is its priority? 4
Please enter the name of the 2nd task: Attend class
What is its priority? 5
Please enter the name of the 3rd task: Go to the movies
What is its priority? 2
TO-DO
---------
Attend class priority: 5
Eat lunch priority: 4
Go to the movies priority: 2

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,396
  • Joined: 27-December 08

Re: prompt a user to enter three tasks and their priorities in a driver

Posted 31 October 2014 - 11:19 PM

The reference this.Task refers to an instance variable. Do you have an instance variable in the Task class named Task?

From this prompt you have: Please enter the name of the 1st task:, it sounds to me like Task should have name and priority as its instance variables.

Note: It is poor practice to name a variable and a class the same thing.

Also, you don't have to quote the post right above you. In fact, please don't, as it makes it harder to weed through the thread. There is a Reply box at the bottom of the thread. :)
Was This Post Helpful? 1
  • +
  • -

#13 MBStevens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-October 14

Re: prompt a user to enter three tasks and their priorities in a driver

Posted 31 October 2014 - 11:26 PM

Sorry, don't know how I didn't notice the big reply button. I'm a little confused as to what you mean about the instance variable. Also, change it to name like this?
 public Task (String name, int priority)
 {
 this.name = name;
 this.priority = priority;
 }

Was This Post Helpful? 0
  • +
  • -

#14 MBStevens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-October 14

Re: prompt a user to enter three tasks and their priorities in a driver

Posted 31 October 2014 - 11:32 PM

I think I got it?
public class Task implements Priority 
{
 private int priority;
 private String name;
 
 public Task (String name, int priority)
 {
 this.name = name;
 this.priority = priority;
 }
 
  public void setPriority(int newPriority){

     if(newPriority >= MIN_PRIORITY && newPriority <= MAX_PRIORITY){

     }
  }
  
  public int getPriority(){
  
   return priority;   
  
 }
}


Still having errors on TaskDriver though
import java.util.Scanner;

public class TaskDriver
{
 public static void main (String [] args)
 {
  int s;
  
  Scanner scan = new Scanner (System.in);
 
  name name1 = new name ("Please enter the name of the 1st task: ");
  s = scan.nextInt();
  
 }
}
error
TaskDriver.java:11: error: cannot find symbol
  name name1 = new name ("Please enter the name of the 1st task: ");
  ^
  symbol:   class name
  location: class TaskDriver
TaskDriver.java:11: error: cannot find symbol
  name name1 = new name ("Please enter the name of the 1st task: ");
                   ^
  symbol:   class name
  location: class TaskDriver
2 errors

Was This Post Helpful? 0
  • +
  • -

#15 Simple_Condolences  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 180
  • Joined: 10-January 10

Re: prompt a user to enter three tasks and their priorities in a driver

Posted 01 November 2014 - 12:41 AM

Do you have a class called name somewhere? If not, then you can't instantiate a name object which is what the name name1 = new name ("Please enter the name of the 1st task: "); is trying to do.

It looks like you're trying to get some user input to name a task. So you'll need a variable created with a type the can store a name/word(s). You also might want to try looking into a Scanner object to read in the information you want after you ask the question you want to ask. Then you can use that variable as a parameter for your Task object.

This might clear up what I said a bit.

//need a variable here, perhaps named n
//need a Scanner object here
//perhaps prompt the user
//the scan in what you're after. <Scanner object name>.next(); should do the trick
//now your Task object call Tast <Task name> = new Task(variable name here, priority here);


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1