Results 1 to 11 of 11
Like Tree4Likes
  • 1 Post By JosAH
  • 1 Post By gimbal2
  • 1 Post By SurfMan
  • 1 Post By SurfMan

Thread: My first Java Program Opinions, Suggestions...

  1. #1
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    17
    Rep Power
    0

    Default My first Java Program Opinions, Suggestions...

    Hello guys,

    I started taking java this semester at school. So yeah this is my first Java program ever. I just want to get some feedback from. Things I did wrong, not so good programming practice...

    Thanks a lot for the help in advance. I know the program might be a little lame is part of my homework.

    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package Letter;
    import java.util.*;
    
    /**
    This class models a simple letter.
    */
    public class Letter
    {    
        static String sender;
        static String recipient;
        static String body;
       
        /**
        Constructs a letter with a given sender and recipient.
        @param from the sender
        @param to the recipient
        */
        public Letter(String from, String to)
        {
            this.sender=from;
            this.recipient=to;
            this.body="";
        }
        
        //Default Constructor
        public Letter()
        {
            sender="Sender person";
            recipient="Recipient name";
            body="";
        }    
        
        /**
        Adds a line to the body of this letter.
        * @param line Adds a to the body of the letter.
        */
        public static void addLine(String line)
        {
            body = body+ line + "\n";
        }
    
        /**
        Gets the text of this letter.
        * @return Returns the body text of the letter.
        */
        public static String getText()
        {       
            String sFinal ="Dear "+ recipient + "\n"+"\n" + 
                            body  + "\n" +
                            "Sincerely" + "\n" +
                            sender+ "\n";
            
            return sFinal ;
        }
        
        /**
        * Program to print the letter.
        */
        public static void printLetter()
        {
            //System.out.print(null);
        }
        
        /** Main code for the letter.
         * 
         * @param args // no need to provide.
         */
        public static void main(String[] args)
        {
            Scanner s=new Scanner(System.in);
            //Propmt the user for the sender and recipient and 
            //store them in the apropiate variables.
            System.out.println("Please enter the Sender name:");
            sender = s.nextLine();
            System.out.println("Please eneter the Recipient name:");
            recipient = s.nextLine();
            
            //Body of the letter.
            System.out.println("Please start typing the body of your letter:");
            body= s.nextLine();
            
            String line="";        
            while(line.compareToIgnoreCase("y")!=0)
            {
                //Check if the user wants to add extra lines.
                System.out.println("If you are done typing press 'y', or keep on typing for an adional line.");
                
                line= s.nextLine();
                if (line.compareTo("y")!=0) 
                {
                    addLine(line);
                }else{
                    break;
                }
            }
            
            //Get the final letter output
           System.out.print(getText());
           
           //I am not sure if you want a physical printout of the letter here?
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: My first Java Program Opinions, Suggestions...

    Why are all those methods and variable static? And why do you have a constructor if you never instantiate a Letter object anywhere?

    kind regards,

    Jos
    fredlo2010 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,016
    Rep Power
    6

    Default Re: My first Java Program Opinions, Suggestions...

    The concept of object oriented programming has not sunk in yet, that much is clear. The first thing any novice to Java will do is make everything static. But that has only one source: not knowing how to work with classes and objects yet. I suggest studying a little more and learning the basics of working with objects, then rewrite the program to remove all the statics. If you do that, I think you'll have something quite good as a first attempt.
    fredlo2010 likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    17
    Rep Power
    0

    Default Re: My first Java Program Opinions, Suggestions...

    Hello guys,

    Thanks a lot for the replies. This is really my second try. bellow is the code for my first try which does not include any Statics other than the main procedure. With that setup the program will not compile and gives me this error

    " Uncompilable source code - non-static variable sender cannot be referenced from a static context
    at Letter.Letter.main(Letter.java:80)"

    I was reading in other forums and the way to solve this issue s to create an instance of the class so I went and did that but then other small programs within the class will not work with this new instance...I think maybe I am not thinking through this properly.

    @JosAH: I have a constructor there because its an assignment requirement.

    This is the other code without any statics.

    Java Code:
    package Letter;
    import java.util.*;
    
    /**
    This class models a simple letter.
    */
    public class Letter
    {    
        private String sender;
        private String recipient;
        private String body;
       
        /**
        Constructs a letter with a given sender and recipient.
        @param from the sender
        @param to the recipient
        */
        public Letter(String from, String to)
        {
            this.sender=from;
            this.recipient=to;
            this.body="";
        }
        
        //Default Constructor
        public Letter()
        {
            sender="Sender person";
            recipient="Recipient name";
            body="";
        }    
        
        /**
        Adds a line to the body of this letter.
        * @param line Adds a to the body of the letter.
        */
        public  void addLine(String line)
        {
            body = body+ line + "\n";
        }
    
        /**
        Gets the text of this letter.
        * @return Returns the body text of the letter.
        */
        public String getText()
        {       
            String sFinal ="Dear "+ recipient + "\n"+"\n" + 
                            body  + "\n" +
                            "Sincerely" + "\n" +
                            sender+ "\n";
            
            return sFinal ;
        }
        
        /**
        * Program to print the letter.
        */
        public static void printLetter()
        {
            //System.out.print(null);
        }
        
        /** Main code for the letter.
         * 
         * @param args // no need to provide.
         */
        public static void main(String[] args)
        {
            Scanner s=new Scanner(System.in);
            //Propmt the user for the sender and recipient and 
            //store them in the apropiate variables.
            System.out.println("Please enter the Sender name:");
            sender = s.nextLine();
            System.out.println("Please eneter the Recipient name:");
            recipient = s.nextLine();
            
            //Body of the letter.
            System.out.println("Please start typing the body of your letter:");
            body= s.nextLine();
            
            String line="";        
            while(line.compareToIgnoreCase("y")!=0)
            {
                //Check if the user wants to add extra lines.
                System.out.println("If you are done typing press 'y', or keep on typing for an adional line.");
                
                line= s.nextLine();
                if (line.compareTo("y")!=0) 
                {
                    addLine(line);
                }else{
                    break;
                }
            }
            
            //Get the final letter output
           System.out.print(getText());
           
           //I am not sure if you want a physical printout of the letter here?
        }
    }
    BTY I really appreciate the response. This is my third programming language. But I sort of skipped C++ so I can transfer to another school after this. The course started kinda fast thought I have to catch up :)

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

    Default Re: My first Java Program Opinions, Suggestions...

    In your main, you should create a new Letter object (that's when the constructor kicks in) and use the methods addLine(...) etc on that object.
    I'll give you a head start:
    Java Code:
    public static void main(String[] args) {
       Letter myLetter = new Letter();
       ....
    
    }
    fredlo2010 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    17
    Rep Power
    0

    Default Re: My first Java Program Opinions, Suggestions...

    Hello,

    I created the new Letter instance and pointed the information to load its attributes. But now I have the same "non-static referring to static error" in this case pointing to my methods:

    addLine(line);
    and
    getText()

    This is what I have so far.

    Java Code:
    package Letter;
    import java.util.*;
     
    /**
    This class models a simple letter.
    */
    public class Letter
    {    
        private String sender;
        private String recipient;
        private String body;
        
        /**
        Constructs a letter with a given sender and recipient.
        @param from the sender
        @param to the recipient
        */
        public Letter(String from, String to)
        {
            this.sender=from;
            this.recipient=to;
            this.body="";
        }
         
        //Default Constructor
        public Letter()
        {
            sender="Sender person";
            recipient="Recipient name";
            body="";
        }    
         
        /**
        Adds a line to the body of this letter.
        * @param line Adds a to the body of the letter.
        */
        public  void addLine(String line)
        {
            body = body+ line + "\n";
        }
     
        /**
        Gets the text of this letter.
        * @return Returns the body text of the letter.
        */
        public String getText()
        {       
            String sFinal ="Dear "+ recipient + "\n"+"\n" + 
                            body  + "\n" +
                            "Sincerely" + "\n" +
                            sender+ "\n";
             
            return sFinal ;
        }
         
        /**
        * Program to print the letter.
        */
        public void printLetter()
        {
            //System.out.print(null);
        }
         
        /** Main code for the letter.
         * 
         * @param args // no need to provide.
         */
        public static void main(String[] args)
        {
            Letter myLetter= new Letter();
            Scanner s=new Scanner(System.in);
            //Propmt the user for the sender and recipient and 
            //store them in the apropiate variables.
            System.out.println("Please enter the Sender name:");
            myLetter.sender = s.nextLine();
            System.out.println("Please eneter the Recipient name:");
            myLetter.recipient = s.nextLine();
             
            //Body of the letter.
            System.out.println("Please start typing the body of your letter:");
            myLetter.body= s.nextLine();
             
            String line="";        
            while(line.compareToIgnoreCase("y")!=0)
            {
                //Check if the user wants to add extra lines.
                System.out.println("If you are done typing press 'y', or keep on typing for an adional line.");
                 
                line= s.nextLine();
                if (line.compareTo("y")!=0) 
                {
                    addLine(line);
                }else{
                    break;
                }
            }
             
            //Get the final letter output
           System.out.print(getText());
            
           //I am not sure if you want a physical printout of the letter here?
        }
    }
    Thanks a lot for the help. I just need to pass that argument to the methods.

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

    Default Re: My first Java Program Opinions, Suggestions...

    That is because those are instance methods, that only work on an instance. Try executing these methods on your Letter instance. (Hint: myletter.addLine(...))
    fredlo2010 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    17
    Rep Power
    0

    Default Re: My first Java Program Opinions, Suggestions...

    Hello,

    @SurftMan: you are the best!!!

    That did the trick. Its all working and running now. Of course those are class methods. I am so used to have the class and the main code in different places that I got lost.

    This is my final code. Working as intended.

    Java Code:
    package Letter;
    import java.util.*;
     
    /**
    This class models a simple letter.
    */
    public class Letter
    {    
        private String sender;
        private String recipient;
        private String body;
        
        /**
        Constructs a letter with a given sender and recipient.
        @param from the sender
        @param to the recipient
        */
        public Letter(String from, String to)
        {
            this.sender=from;
            this.recipient=to;
            this.body="";
        }
         
        //Default Constructor
        public Letter()
        {
            sender="Sender person";
            recipient="Recipient name";
            body="";
        }    
         
        /**
        Adds a line to the body of this letter.
        * @param line Adds a to the body of the letter.
        */
        public  void addLine(String line)
        {
            body = body+ line + "\n";
        }
     
        /**
        Gets the text of this letter.
        * @return Returns the body text of the letter.
        */
        public String getText()
        {       
            String sFinal ="Dear "+ recipient + "\n"+"\n" + 
                            body  + "\n"+
                            "Sincerely" + "\n" +
                            sender+ "\n";
             
            return sFinal ;
        }
         
        /**
        * Program to print the letter.
        */
        public void printLetter()
        {
            //System.out.print(null);
        }
         
        /** Main code for the letter.
         * 
         * @param args // no need to provide.
         */
        public static void main(String[] args)
        {
            Letter myLetter= new Letter();
            Scanner s=new Scanner(System.in);
            //Propmt the user for the sender and recipient and 
            //store them in the apropiate variables.
            System.out.println("Please enter the Sender name:");
            myLetter.sender = s.nextLine();
            System.out.println("Please eneter the Recipient name:");
            myLetter.recipient = s.nextLine();
             
            //Body of the letter.
            System.out.println("Please start typing the body of your letter:");
            myLetter.body= s.nextLine()+ "\n";
             
            String line="";        
            while(line.compareToIgnoreCase("y")!=0)
            {
                //Check if the user wants to add extra lines.
                System.out.println("If you are done typing press 'y', or keep on typing for an adional line.");
                 
                line= s.nextLine();
                if (line.compareTo("y")!=0) 
                {
                    myLetter.addLine(line);
                }else{
                    break;
                }
            }
             
            //Get the final letter output
           System.out.print(myLetter.getText());
            
           //I am not sure if you want a physical printout of the letter here?
        }
    }
    Thanks a lot guys :)

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,016
    Rep Power
    6

    Default Re: My first Java Program Opinions, Suggestions...

    Yes it works - but do you understand WHY it works? If not, check that quote in SurfDude's signature.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    17
    Rep Power
    0

    Default Re: My first Java Program Opinions, Suggestions...

    I there a way to mark this thread solved in this forum? I was looking for it and I cannot find it.

    This is a solved one for sure :)

  11. #11
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    17
    Rep Power
    0

    Default Re: My first Java Program Opinions, Suggestions...

    Quote Originally Posted by gimbal2 View Post
    Yes it works - but do you understand WHY it works? If not, check that quote in SurfDude's signature.
    Hi Gimbal2,

    Yes I know why it works. The issue here is that I am used to .Net where classes are in a different files so Class Methods do not get mixed up with Application programs.

    So I created an instance of the class and then I am feeding an updating its properties (attributes) through it methods as I am running my main Procedure.

    Thanks a lot. If you have a better explanation or want to elaborate, please by all means...I am all ears. :)

Similar Threads

  1. Java job suggestions - best path to take?
    By TJ500 in forum Jobs Discussion
    Replies: 0
    Last Post: 12-24-2012, 05:34 PM
  2. College "java" paper.....a couple opinions?
    By hayden06f4i in forum New To Java
    Replies: 6
    Last Post: 11-01-2010, 07:15 AM
  3. SwingWorker Opinions
    By frejon26 in forum AWT / Swing
    Replies: 3
    Last Post: 04-13-2009, 08:41 PM
  4. Suggestions for my Java-project?
    By shabbee in forum New To Java
    Replies: 1
    Last Post: 01-01-2009, 10:11 PM
  5. Opinions on best approach for objects
    By spikey in forum Advanced Java
    Replies: 1
    Last Post: 04-11-2008, 05:20 PM

Posting Permissions

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