Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default java logic for tagmaker program

    so i've taken an intro to programming class (using c++) a year ago--and it's safe to say i've forgotten a lot of it. i'm taking an object-oriented java course and i've tried creating a few programs with little success. i've decided to go back to basics and start using pseudocode--but i want to see if my logic is correct.

    so here's the first program:

    each delegate to a certain conference must wear a tag showing their name and the organization they represent as shown below:

    ######################
    #### Annual Conference #####
    ######################
    ##NAME: ##
    ######################
    ##ORGANIZATION: ##
    ######################

    write a class called TagMaker that prints out tags. supply methods to (a) set the name (b) set the organization (c) print tag with the name and organization (d) clear the name and organization (e) print a blank tag. then write a TagTester class to test the TagMaker class.


    so this is the pseudo code (or logic) that i've come up with for the program:

    TagMaker main class:
    Prompt user to enter in first and last name, as well as their organization.
    Read user input for the name and organization.
    Return user input.
    Write conference tag showing the person's information.
    Clear information.
    Print blank tag.


    1. i assume i would have to import a few java libraries. i'm pretty sure i would use
    Java Code:
    java.util.Scanner
    and
    Java Code:
    java.io.*;
    2. afterwards i would declare the instance variables (private, i assume). static too? so for example:
    Java Code:
    private static string firstName
    3. i'm not sure if this program requires a constructor...being that i'm supposed to clear the program at the end anyway.

    4. i create a public class declaring my input variable. example:
    Java Code:
    String first = firstName.nextLine();
    5. so i then i have to create methods that get/set the user input.

    getter/setter example:

    Java Code:
              public String getFirst(){
                return first;
            }
              public void setFirst(){
                  this.first = new first;  
              }
    6. to print out both tags, would i write most of the code as part of a loop and make an "if" statement where the user input is valid, it prints out a tag with information, and another "if" statement that would automatically clear the buffer? this part confuses me.

    7. last i would create a tester class that would primarily be responsible for the program's output.


    ta-da? please help :/. i did try writing the code too but all i get are compiler errors so i felt the need to start from scratch.

  2. #2
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    179
    Rep Power
    0

    Default Re: java logic for tagmaker program

    You have, currently, for #5:
    Java Code:
    public String getFirst(){
        return first;
    }
      public void setFirst(){
          this.first = new first; 
      }
    Unless I'm misunderstanding the setup that you're looking for, your setter isn't completely correct.
    The variables that are this.variable are class variables. You are correct, that you use a setter function to change those variables to something new in an instance, but you need to pass along what you want to set it to.
    In other words, one of the parameters for the setter must be, String first
    That way, you know what you are making this.variable.

    If you wanted to do a very simple program, you could simply
    1) Ask for the name, save the name to a string
    2) Ask for the organization, save the organization to a string
    3) Print out a line of #s, followed by 2#s, name, 2#s, new line of #s, etc. until the whole thing is printed
    4) Change name to empty
    5) change organization to empty
    6) repeat step 3

    This is the way a student in an intro class would do it - all in the main method.

    From what I can see, you could also create a printTag() method that does the printing, and the setting methods (as well as a constructor if you want to do it in a separate class).

    So maybe,
    (optional first step - write a separate class for the tag creation - would be better if you had a large amount of people to write tags for, and if you wanted to reference to various tags. Would be done by creating a new class called TagMaker, then doing TagMaker smith = new TagMaker(); though you could do this in the same class)
    1) Get name from user, setName(name)
    2) Get organization from user, setOrg(organization)
    3) printTag() (which will use the oran and name you just set
    4) setName(""), setOrg("")
    5) printTag()
    Last edited by AlexGraal; 03-02-2014 at 06:30 AM.

  3. #3
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Re: java logic for tagmaker program

    so would i write it like this:

    Java Code:
              public void setFirst(){
                  this.first = new String (first);  
              }
    or like this:

    Java Code:
              public void setFirst(){
                  this.first = first;  
              }
    or did i get both wrong?

  4. #4
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    179
    Rep Power
    0

    Default Re: java logic for tagmaker program

    Both of those are wrong, since I don't think you understand what parameters are.

    You can read about them here: Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    They are basically the information you pass along to your class.

    Your second example is closest:
    Java Code:
    public void setFirst(){
        this.first = first; 
    }
    We assume that this.first is a instance variable.
    But what are you setting this.first equal to. What is the value for first? You didn't pass any value along to your set method.

    I guess you could think about it like a dialogue between two people? If you are talking to your friend and ask him to change the channel, but neglect to mention which channel to change it to, how does he know what channel to go to? You have to pass along the parameter - the actual channel number/name - so that your friend can change the channel.
    Last edited by AlexGraal; 03-03-2014 at 03:56 PM.

  5. #5
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: java logic for tagmaker program

    I believe AlexGraal means that this.first is referencing an instance variable and not a class variable, class variables have the static modifier and are created only once for each class and not each instance of the class. The firstName variable does not need to be static either. You would make it private String firstName; and then use a public getter/setter to access it, also known as encapsulation.
    Java Code:
    public void setFirst(String first){
    	this.first = first;
    }

  6. #6
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Re: java logic for tagmaker program

    okay, so i tried setting it up. i got the code to accept the user input and print out a tag...but i did it without a tester class (i'm scared of those, and it wasn't working so i stopped using it. any suggestions there?) and i've experimenting with codes that would clear the scanner to print out a blank tag also but it kept messing up the program so i took it out.

    this is what i have so far:

    Java Code:
    package TagMaker;
    
    import java.util.Scanner;
    //import java.util.Locale;
    //import java.io.*;
    
    public class TagMaker {
    
       public static void main (String[] args)
     {
       Scanner scannerObject = new Scanner( System.in );
    
       
         System.out.print("This program will print out a name tag");
         System.out.println("for each delegate.");
         System.out.println("Please enter first name:");
         String first = scannerObject.next();
         System.out.println("Please enter last name:");
         String last = scannerObject.next();
         System.out.println("Please enter organization or affilation:");
         String org = scannerObject.next();
         
    
    	System.out.println("###### " + "Annual Conference" + " ######");
            System.out.println("### NAME: " + first + " " + last + " ###");
            System.out.println("################################");
            System.out.println("### ORGANIZATION:" + org + "###");
            System.out.println("###############################");
    
    
    		String junk = scannerObject.next();
    	}
    }
    i tried looking in my textbook for something that would work but nothing i've tried has worked so far ): i also read something about a clone method that seemed like it *might* work (unless i've misunderstood it's function) but it's in chapter 13 and my class is only in chapter 6 right now.

  7. #7
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Re: java logic for tagmaker program

    would it work if i wrote an if/then statement? like if the program accepts the user input and prints out a delegate's tag, then it would print out another tag with no names?

Similar Threads

  1. need program logic
    By anonymouss in forum New To Java
    Replies: 59
    Last Post: 05-08-2013, 10:07 AM
  2. how is the logic this program???
    By riris in forum New To Java
    Replies: 4
    Last Post: 03-16-2012, 05:38 PM
  3. Need help on this program logic
    By cotemaria in forum New To Java
    Replies: 1
    Last Post: 11-12-2011, 09:14 PM
  4. need help in merging GUI and logic main program
    By lavender88 in forum NetBeans
    Replies: 2
    Last Post: 04-02-2011, 01:28 PM
  5. Separating program logic and GUI
    By loopingman in forum AWT / Swing
    Replies: 0
    Last Post: 12-08-2008, 02:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •