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
+1 Jacob Fellows · December 10, 2014
Yes sorry its just that I thought you weren't going to be helpful after what you said. :)
+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.
+1 Jacob Fellows · December 15, 2014
Hi there,

It didn't go well and I was kind of hoping for more help rather than a list of resources to go look at. I was hoping for help as in putting our minds together and thinking of a solution to my problem one step at a time. The program I built runs and displays the first message and then a referral message to the end of January.

I think I am going to email the course leader tomorrow to see if I can sit with him and ask him for the help I need and to see if there is anything we can do together to get me back up and running with my mind set so that I love java and will give it a shot in the barrel if I have too.

The reason I was wanting help is that I struggle to have any kind of inspiration or something to look forward too after I have done this course, my head isn't in the right place at the moment and I suffer in silence from bad personal mistakes and decisions.

Thanks for trying to be helpful I really appreciate it and would like you to help me out in future with any problems I have. Hopefully you can help me understand it with examples of code that I could use.

On a positive note have a great Christmas and new year!!! Also look out for a post from me when you are not busy with family over Christmas as I may need help.

Thanks from a tired out and needing to be inspired student,
Jacob.

PS sorry for not replying so soon busy with other assignments!
+1 Jacob Fellows · December 16, 2014
Fair play to you. Many people look at these forums and don't help out people if they need it and that is what really annoys me as I have used another forum site called whirlpool and the forums are really dead on that site as people don't want to help each other.

Anyways have a Great Christmas!!!

Thanks!!!8-)
+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 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
0 Developer John · December 9, 2014
I've been looking at your code and using my resources right now. It might take me some time to figure out your request. It shouldn't be too long though.
0 Jacob Fellows · December 9, 2014
^^^Create yourself a thread as that just seems unfair that you can go onto someone elses thread and then post your problems, Please it is really simple. Anyways my problem i'm still having trouble with.
0 Developer John · December 9, 2014
First off, lets go over the main problems I seen in your code before I can start organizing your request. Are you aware of the errors you get when using your object variable in the switch statement(ad.set(), ad.get(), ad.add())? Secondly, in your switch statement, why do you go from one to six(lol)? Another problem in your code I noticed was that you used a char for the user input. What I suggest is making it a String instead and using:
while(theNameOfTheNewVariable.equalsIgnoreCase("Y");

Lastly, I don't understand your boolean call isEmpty. Why did you return n == 0?

Here is you're code, but a bit more indented lol:

Class DeQue:

import java.util.UUID;

/**
 *
 * @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


Assignment2ArrayDeQuePrintJob:

import java.util.ArrayDeque;
import java.util.Scanner;

/**
 *
 * @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");
        
        //This is declaring a new array dequeue function.
        
        ArrayDeque ad = new ArrayDeque();
        
        //This is used throughout the cases on this java program.
        
        String answer;
        
        //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 within 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("Enter index");
            ad.remove(scan.nextInt()); 
            break;       
        case 5:
            System.out.println("Empty Status: " + ad.isEmpty());
            break;                                                 
        case 2:
            System.out.println("Array Deque Cleared");
            ad.clear();
            break;       
        default:
            System.out.println("Wrong Entry");
            break;  
       }   
        //This action is the way you exit out of the program at any point.
        
        System.out.println("Do you want to continue using the program (Type y or n)");
        
        answer = scan.nextLine();                        
       }
        //This action will take place when you say yes to continue on with the
        //program and when you say no it will end the program.
        
        while(answer.equalsIgnoreCase("Y"));
        
        scan.close();
    } //End of the main method
    
} //End of the class


Keep in mind, I haven't started on your request yet. I need these questions answered before I can get started. Some changes I've made to your code though:

-Obviously indentation
-Made the println statements not have \n only if they were printing out one String
-Improved the do while loop by making a String called answer, and then made it .equalsIgnoreCase inside of the while loop
-Made your Scanner close before the end of the method body
-I've imported all the necessary utilities you need for your program
-I've also lined up your println statement at the beginning of the do while loop that has multiple Strings in it, so that you can see the actual format it will be on the screen.

I expect my questions to be answered lol.:sideways:

Java / Android Development

107,090 followers
About

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

Links
Moderators
Bucky Roberts Administrator