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 2
Oldest  Newest  Rating
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:
0 Jacob Fellows · December 9, 2014
Hi there, I found the code online and wanted to do something similar and wanted to change it to make it function the way I want it too. I struggle with programming and I don't enjoy it because my lecturer seems to be doing things in a very odd order. Such as in my first assignment we were meant to use javadocs comments and I didn't get it and then two weeks ago we learn about it.

Also the code is to be used for a print job. The UUID is a way of creating a unique identifier for the printer ID.

Here is where I got the code from: http://www.sanfoundry.com/java-program-array-deque/

I just thought I would change it so that it makes sense for the program in mind. Also I am using the scanner so that it can take inputs and display outputs in the same box.

1. I am not aware of what the ad.set, ad.get or ad.add are.
2. I need to organise the switch to match the block of methods you can do in the program. maybe move them around so that they work good.
3. The Boolean is used as a true or false statement and probably be changed to an if statement or something so that it works. Also if the array is 0 then it means that it is empty and needs data to be inputted from the user.
4. Also in the java class there needs to be a method for adding a instance of the object, setting the head and tail of the array queue, display the array deque with all the values inputted into it and a way to clear the data from the array deque.

Thanks for the help my dude! It boosts my spirits and makes me feel as if I need to do something nice for you.

Thanks,
Jacob
0 Developer John · December 9, 2014
Lol, no problem. I usually help people with their code lots of times outside these forums. I'm sorry for saying this, but I don't think it's a great idea copying code. I feel like it's better to make your own unique program so that someone gets ideas from you instead. When I copy someone's code, I feel like I don't know what I'm doing, or I'm just bored lol.:sideways:
0 Jacob Fellows · December 9, 2014
Hi There Developer John,

I thought you were going to be my little helper but I guess not. I have spent hours trying to find an example of the sort of code that I needed for the assignment so that I could use it as an example and make sure that I build the correct way. So yeah, I need the help and expertise of someone that has more java knowledge than me. I will always try to help others out and have found this site to be better at responding to a thread or post that someone has created over whirlpool and many like it. I also prefer this forum because we can have fun and see what people need help with.

Thanks from a tired, worried student named Jacob.
0 Developer John · December 9, 2014
Lol, now you sound desperate. I never really said I was giving up, I just wanted to share my thoughts on this. So, shall we continue?:sideways:
+1 Jacob Fellows · December 10, 2014
Yes sorry its just that I thought you weren't going to be helpful after what you said. :)
0 Developer John · December 10, 2014
Okay, I understand. So, how exactly do you want to change this program? Have you had much experience with Queues in Java lately?;)
0 Jacob Fellows · December 11, 2014
Hi there,

The assignment is meant to be in by 11pm UK time tonight so I am going to just use code I already have and manipulate it so that it works. I haven't used an array deque before and I have looked into how a normal queue works. I would still like the help though.

I would like to manipulate the code so that it allows the user to add a document name. file size, user ID and printer ID.

Thanks from a stupid student named Jacob.

Java / Android Development

107,319 followers
About

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

Links
Moderators
Bucky Roberts Administrator