HELP! JAVA CLASS, QUEUES And Scanners

+3 Jacob Fellows · December 6, 2014
Hi there,
I'm struggling with my assignment on queues that is to create a program that creates a print queue and am allowed to use any method such as a linked list, dequeue or the queue<E> function. I would rather use the queue function as it seems simple.
I have attached the java class code I have done so far and I am learning to use these code boxes as well.

The java class code:
package bobsyouruncleassignment2;
import java.util.*;
/**
 *
 * @author Jacob
 */
public class DeQueCode
{ //Start of class
    int fileSize;
    private String fileName;
    private String userID;
    private UUID printerID;
   
    /**
    *This is the constructor of the JavaDoc for my program
    * It is used to create a new print job.
    *@param fileSize is the file size
    *@param fileName is the file name
    *@param userID is the user Identification
    * @param printerID is the identification for the printer
    */
    public DeQueCode(int fileSize, String fileName, String userID, UUID printerID)
    { //The word *this.* means set the value in the instance of a print job
        this.fileSize = fileSize;
        this.fileName = fileName;
        this.userID = userID;
        this.printerID = printerID;
    } //End of the constructor
   
    //------------------------------------------------------------------------------
    //Now to the methods that are going to be used in my program
    //------------------------------------------------------------------------------
   
    //Starting with the getters
    /**
     * Gets the file name
     */
    public int getFileSize()
    {
        return this.fileSize;
    }
   
    /**
     * Gets the last name
     */
      public String getFileName()
    {
        return this.fileName;
    }
     
    /**
     * Gets the telephone number
     */
        public String getUserID()
    {
        return this.userID;
    }
   
        /*
        *Gets the printer ID.
        */
        public UUID getPrinterID()
        {
            return this.printerID;
        }
       
        //Now the setters
        /**
         * Sets the print job for the file size
         * @param fs file size of the print job
         */
       
    public void setFileSize (int fs)
    {
        this.fileSize = fs;
    }
   
        /**
         * Sets the print job for the file name
         * @param fn file name of print job
         */
       public void setFileName (String fn)
    {
        this.fileName = fn;
    }
      
        /**
         * Sets the print jobs user name
         * @param ui user ID for the print job
         */
       public void setUserID (String ui)
    {
        this.userID = ui;
    }
       /**
        * Sets the print job a random printer ID
        * @param
        */
       public void setPrinterID (UUID pi)
       {
           this.printerID = pi;
       }
      
       //Finally the toString method to show all the print jobs info
       @Override
     public String toString()
     {
         return ("File sizee: " + fileSize + "\n"
                 + "File name:" + fileName + "\n"
                 + "User ID: " + userID + "\n"
                 + "Printer ID: " + printerID);
     }
            
} //End of class


I would like to then make the program itself have a few option choices such as the code below that will go along with the above.
    public static void main(String[] args) {
       
    Queue q = new Queue(); //Queue of size 5
    int ch;
    Scanner sc =new Scanner(System.in);
    do
    {
    System.out.println("Menu\n1.insert\n2,delete\n3.display\n4.destroy\n5.exit\n");
    System.out.println("Enter choice :");
    ch=sc.nextInt();
    switch(ch)
    {
        case 1:
            System.out.println("Enter data to insert");
        int x=sc.nextInt();
        q.insert(x);
        break;
        case 2:
            if(q.empty())
            System.out.println("Queue underflow");
            else
            {
                int z =q.delete();
                System.out.println("data deleted =" + z );
            }
            break;
        case 3: q.display();
            break;
        case 4: q.destroy();
            break;
        case 5: break;
        default : System.out.println("Wrong Choice");
    }
    }while(ch!=5);
    }
   
}

Please help and all the help is greatly appreciated from you guys as you seem to know stuff better than I do and I would love to become part of your java crew.
Thanks again,
Jacob.
P.S If you do help me Santa will be very happy with you this year! :) LOL

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Developer John · December 7, 2014
You'll learn your mistake.
+1 Jacob Fellows · December 7, 2014
I learn that people on the internet are nice and will guide me in correct direction rather than not respond at all. It will help me in the future as well because I would be able to do that and have an example of that code.

Also are any of you going to help or will I just have to suffer and figure out my own way of merging the two bits of code together.

Plus Java isn't my best subject and I would rather do something like networking or graphic design so that I can do other things in the future. But on my course I have to study Java which can be a real pain in the bum sometimes.

EDIT: I have started merging a few bits of code together and this is what I got so far.
Java class:
package assignment2arraydequeprintjob;
import java.util.*;
/**
 *
 * @author Jacob
 */
class ArrayDeQue
{ //Start of class
   private int fileSize;
   private String fileName;
   private String userID;
   private UUID printerID;
   private int n;
  public ArrayDeQue()
  { //Start of constructor
    fileSize = 0;
    fileName = "";
    userID = "";
    n = 0;
    } //end of the constructor
 
//This will check to see if the array queue is empty or not.
        public boolean isEmpty()
    {
            return n == 0;
    }
       
//This will make the array queue empty of values.
        public void clear()
    {
           n = 0;
           fileSize = 0;
           fileName = null;
           userID = null;
    }
       
} //End of class

Main class of the program:

package assignment2arraydequeprintjob;
import java.util.*;
/**
 *
 * @author Jacob
 */
public class Assignment2ArrayDeQuePrintJob
{ //Start of the class
    public static void main(String[] args)
    { //Start of the main method
        //Declaring a scanner to get inputs from the output box.
         Scanner scan = new Scanner(System.in);
        //This is the welcome message to my program.
           System.out.println("Welcome to the Print Job Program\n");
        //This is declaring a new array deque function.
         ArrayDeque ad = new ArrayDeque();
        //This is used throughout the cases on this java program.
         char ch;
        //This is the message that will be displayed so that the person can\n
         //decide what they want to do when the program runs.
          do
    {
        //Declaring the names of the cases that are going to be below this.
          System.out.println("Print Job Operations\n"
                  + "1.insert\n"
                  + "2,delete\n"
                  + "3.display\n"
                  + "4.destroy\n"
                  + "5.Array empty or not\n"
                  + "6.exit\n");
        //Below is what we use to iterate through the array and what we do to \n
         //use the functions above withn this code.
             int choice = scan.nextInt();           
          switch (choice)
          {
          case 1 :
        System.out.println("Enter index and element");
            ad.add(scan.nextInt(), scan.nextInt() );                    
            break;                         
        case 6 :
             System.out.println("Enter index");
             System.out.println("Result : "+ ad.get(scan.nextInt() ));
         break;       
     case 3 :
          System.out.println("Enter index and element");
          ad.set(scan.nextInt(), scan.nextInt() ); 
              break;
        case 4 :
             System.out.println("\nEnter index");
            ad.remove(scan.nextInt() ); 
             break;       
         case 5 :
               System.out.println("\nEmpty Status : "+ ad.isEmpty());
               break;                                                 
          case 2 :
                System.out.println("\nArray Deque Cleared");
               ad.clear();
               break;       
          default :
               System.out.println("Wrong Entry \n ");
                break;  
            }  
         //This action is the way you exit out of the program at any point.
System.out.println("\n Do you want to continue using the program (Type y or n) \n");
            ch = scan.next().charAt(0);                       
    }
        //This action will take place when you say yes to continue on with the\n
         //program and when you say no it will end the program.
          while (ch == 'Y'|| ch == 'y');
       
    } //End of the main method
   
} //End of the class

I just need someone to help me figure a way of making it so that it can use strings and a unique code for the printerID.
Everything you do is hugely appreciated and makes me feel happy that I can talk to someone that knows java inside out as I don't particular want to talk to my lecturer.
Thanks,
Jacob.
0 Developer John · December 7, 2014
I'll try to help you lel:
1. Importing many classes in a Java program could result in some lag issues down the road
2. When doing:
System.out.println("Welcome to the Print Job Program\n");

The \n is unnecessary, because println goes to the next line after printing out something, but only do this when printing out one primitive datatype or variable.
3. When you close the program, why not use an if statement to check if the user input is equal to "y"? Right after that, you could make a statement where the Scanner closes:
ScannerName.close();

4. I don't know why, but I'm having a hard time trying to figure out what you want this program to do next.
 
0 Jacob Fellows · December 7, 2014
Hi there,

The program is meant to be a print job queue that gets input via a scanner from the user and then puts it into an array queue that you can then delete the head of the queue and add to the end of the queue. Thanks for pointing out the scanner close thing and should I change it to print instead of println?

Thanks,
Jacob.
P.S Does any of the above make any sense???
0 Developer John · December 8, 2014
Yes it makes sense, I just wanted a clear meaning of what you want it to do, since you said this is what I have so far. I don't think it's necessary to change to print, just delete the \n. Now I understand the whole getting user input part of it and storing the user input in an array, but I don't understand the deletion of the head of the queue and addition of the end of the queue part. Do you mind explaining that to me?

-Thanks!:sideways:
+1 Jacob Fellows · December 8, 2014
Well when you are in a shop and you our queuing to buy something the person at the front gets served and then leaves. At the end of the queue people join onto the end of it. Does this help. Also instead of using the Array Deque do you think I should use import java.util.Queue<E> at all???
0 Developer John · December 8, 2014
Yes, it does help. Thanks for explaining it some more!
0 Jacob Fellows · December 8, 2014
Do you think I will need to use import java.util.queue<E> for the program instead of the array deque because we need to order them and then remove or add to the queue. A queue is a type of data structure that you can use and I need to use it. Also there are no examples of the type of thing I need to do as they all contain ints and no one seems to have the courage to try a class containing strings, ints and UUID (Random generated text for the printer ID

if you hit a wall just let me know.
0 Ledio Ballhysa · December 8, 2014
Hello.I am currently a newbie in Java and I was wondering if smn would help me for this project.
Thanks.
JAVA PROGRAMM
Build a class named Rectangle that represents a two - dimensional area . This class
must contain:
public Rectangle ( int x , int y , int width, int length)
-Constructs a rectangle that has the height of the upper - left coordinates x , y , and size as width and length:
public Rectangle ( Point p , int width , int length)
-Builds a rectangle that has the height of the upper - left point p and with sizes as width and length.
( Point is a class that you have to build that contains two fields that define the position of a point in the coordinate plan ) .
public int getWidth ( )
public int getGjat ( )
public int getX ( )
public int getY ( )
public boolean contains ( int x , int y )
-Returns true if the point with coordinates x , y is located within the rectangle .
public boolean contains ( Point p )
Returns true if the point p is inside the rectangle .
public Rectangle cross ( Rectangle r )
-Returns a new rectangle that represents the largest surface that is contained to this rectangle and to the other rectangle r . If rectangulars does not cross at all, should be returned a rectangle with width and length equal to 0 :
public String toString ( )
-Returns a string representation of the rectangle ,shaped in the form (for example):
" Rectangle [ x = 1 , y = 2 ,width = 3 , length = 6 ] " .
-Build an application that tests the Rectangle class .
0 Developer John · December 9, 2014
Umm... lol

Java / Android Development

106,913 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator