Advice on techniques for writing code (my first semester)

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 1221 Views - Last Post: 15 October 2013 - 10:45 AM Rate Topic: -----

#1 jphoc13  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 166
  • Joined: 08-July 13

Advice on techniques for writing code (my first semester)

Posted 13 October 2013 - 12:24 PM

So I think my biggest issue is that when I start an app assignment I get bogged down by seeing errors with almost every line of code I type. I think most of these may just go away as I continue to write the app, but I keep thinking to myself that if they don't go away then I am going to spend a ton of time rewriting all the code I just did.

It is easy to do a UML diagram to get the idea of what I should do but putting the code in gets me bogged down in thinking I will have a hundred errors by the time I finish my program.

For example:


package employee;

/**
 *
 * @author Derek
 */
public class Employee {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String name;
        int idNumber;
        String department;
        String position;
        
    public Employee (String employeeName, int idNum, String dpt,
            String pos)
            {
                name = employeeName;
                idNumber = idnum;
                department = dpt;
                position = pos;
                       
            }
    }
}



Should I just keep going even though my constructor has errors because of variables it doesn't recognize (can't find symbol error)?

I am following the examples in my textbook exactly but I think to myself that I am doing something wrong right from the get go.

SO should I just code out the app, assuming these errors will go away? Or are they there already for a reason?

If you need details on the code let me know, but this is more just a general question on technique.

Is This A Good Question/Topic? 0
  • +

Replies To: Advice on techniques for writing code (my first semester)

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3223
  • View blog
  • Posts: 10,810
  • Joined: 12-December 12

Re: Advice on techniques for writing code (my first semester)

Posted 13 October 2013 - 12:37 PM

Run, or at least Build, your code frequently to correct errors as you progress.

A good Editor/IDE can also show error-indicators - underlining and providing error prompts and corrections. However, many programmers are not in favour of such assistance (and you may be obliged to use a particular editor anyway.)

A good way to ensure that you can frequently run or build your application is to fill-out all the scaffolding first. That is, create empty functions, methods and classes, and always type the closing bracket as you progress. If a function is going to return an integer, but you haven't completed it yet, just use return 4;, or some other placeholder, or appropriate, number.

You will get to recognise popular errors and you will create them less often as you progress ;)
Was This Post Helpful? 3
  • +
  • -

#3 ChrisNt  Icon User is offline

  • D.I.C Addict

Reputation: 200
  • View blog
  • Posts: 623
  • Joined: 31-July 13

Re: Advice on techniques for writing code (my first semester)

Posted 13 October 2013 - 12:38 PM

What i always do is check if my current code works as is supposed to before adding more code.

Main method should only start your application.Here you have local variables in your main method and their scope is only in your main method thats why you get the error can't find symbol.You should define them as fields and in your main method just create the Employee object.
Was This Post Helpful? 1
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3223
  • View blog
  • Posts: 10,810
  • Joined: 12-December 12

Re: Advice on techniques for writing code (my first semester)

Posted 13 October 2013 - 12:42 PM

For your current, and future, errors ;) you should post the full error message and tell us what line it refers to in your posted code. These errors message are there for a reason, they contain a lot of useful information.
Was This Post Helpful? 1
  • +
  • -

#5 jphoc13  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 166
  • Joined: 08-July 13

Re: Advice on techniques for writing code (my first semester)

Posted 13 October 2013 - 12:44 PM

I was completely missing this part in my text book :(


private final String position;
    private final String name;
    private final String department;




So I initialized variable instead of creating methods, derp!
Was This Post Helpful? 0
  • +
  • -

#6 jphoc13  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 166
  • Joined: 08-July 13

Re: Advice on techniques for writing code (my first semester)

Posted 13 October 2013 - 12:52 PM

I appreciate all this help! I plan on spending all day doing this application so I will keep all my questions to this thread. I fixed my first errors thanks you you guys and will make sure to list the line of the errors and the exact error. One I keep running into constantly is what I am getting now:


public class Employee {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
    private final String position;  //line 19
    private final String name;
    private final String department;
    private final int idNumber;
        
    public Employee (String employeeName, int idNum, String dpt,
            String pos)
            {
                name = employeeName;
                idNumber = idNum;
                department = dpt;
                position = pos;
                       
            }
    }
}  // line 34




Line 19 I am getting illegal start if expression

Line 34 I am getting class, interface, or enum expected.

When I google this answer (always my first choice befor bugging you guys) I get a reference to brackets. But my brackets seem to be correct. What am I missing here?

This post has been edited by jphoc13: 13 October 2013 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3223
  • View blog
  • Posts: 10,810
  • Joined: 12-December 12

Re: Advice on techniques for writing code (my first semester)

Posted 13 October 2013 - 01:06 PM

Those private variables should be outside of the main method, as described by ChrisNt, they are part of the Class, not the main method.


Currently your main method includes the constructor. The constructor should be outside of main.

If you indent your code consistently it will be much easier to spot these mistakes, and to see the over-all structure of your code.

public class Employee {

    private final String position;  //line 19
    private final String name;
    private final String department;
    private final int idNumber;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
    }

    public Employee(String employeeName, int idNum, String dpt,
                String pos) {
        name = employeeName;
        idNumber = idNum;
        department = dpt;
        position = pos;

    }

}  // line 34

This post has been edited by andrewsw: 13 October 2013 - 01:07 PM

Was This Post Helpful? 1
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7572
  • View blog
  • Posts: 12,717
  • Joined: 19-March 11

Re: Advice on techniques for writing code (my first semester)

Posted 13 October 2013 - 01:07 PM

First of all, kill your error reporting. It's clearly not helping. Most code is in a state of error when it's being created, and if it bothers you to be reminded of that then turn it off or use a real editor instead of an IDE.

Second, write small. I wrote a little project creator script a while back that creates a project directory with an ant build file and some other ancillary stuff. Among other things, it creates a Main class, which initially prints "Hello world" and exits. Since this is clearly a bug, I then make gradual fixes to this until it does what I'm actully looking for. But since I start with running code, and I make small additions and fix them as I go, I keep my programming errors to a minimum. (logic and design errors, I have to catch by higher-level testing, of course)

Third, do your design before you write a line of code. You should have an overall idea of what it is that's going to happen - and as andrewsw says, it's useful to write stub methods that return dummy values that you can test against.
Was This Post Helpful? 2
  • +
  • -

#9 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 190
  • Joined: 13-July 12

Re: Advice on techniques for writing code (my first semester)

Posted 13 October 2013 - 01:49 PM

View Postandrewsw, on 13 October 2013 - 01:06 PM, said:

If you indent your code consistently it will be much easier to spot these mistakes, and to see the over-all structure of your code.

I would also like to add that I like to use the Allman style when I write my code. It helps me to read the code better but I think that most people are probably used to more compact styles like K&R and textbooks use those styles to preserve space and save money.
Was This Post Helpful? 0
  • +
  • -

#10 jphoc13  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 166
  • Joined: 08-July 13

Re: Advice on techniques for writing code (my first semester)

Posted 13 October 2013 - 01:54 PM

View Postandrewsw, on 13 October 2013 - 01:06 PM, said:

Those private variables should be outside of the main method, as described by ChrisNt, they are part of the Class, not the main method.


Currently your main method includes the constructor. The constructor should be outside of main.

If you indent your code consistently it will be much easier to spot these mistakes, and to see the over-all structure of your code.

public class Employee {

    private final String position;  //line 19
    private final String name;
    private final String department;
    private final int idNumber;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
    }

    public Employee(String employeeName, int idNum, String dpt,
                String pos) {
        name = employeeName;
        idNumber = idNum;
        department = dpt;
        position = pos;

    }

}  // line 34

I tried it this way and i still get the same errors.
Was This Post Helpful? 0
  • +
  • -

#11 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 190
  • Joined: 13-July 12

Re: Advice on techniques for writing code (my first semester)

Posted 13 October 2013 - 01:56 PM

Quote

I tried it this way and i still get the same errors.

Please show us the updated code.
Was This Post Helpful? 0
  • +
  • -

#12 jphoc13  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 166
  • Joined: 08-July 13

Re: Advice on techniques for writing code (my first semester)

Posted 13 October 2013 - 01:58 PM

I fixed it :).

Got rid of publc static void main...

This post has been edited by andrewsw: 13 October 2013 - 02:04 PM

Was This Post Helpful? 0
  • +
  • -

#13 ChrisNt  Icon User is offline

  • D.I.C Addict

Reputation: 200
  • View blog
  • Posts: 623
  • Joined: 31-July 13

Re: Advice on techniques for writing code (my first semester)

Posted 13 October 2013 - 01:58 PM

Save and compile again.Then run your program.

Quote

Got rid of publc static void main...

no way thats for sure not the problem.

This post has been edited by ChrisNt: 13 October 2013 - 02:00 PM

Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3223
  • View blog
  • Posts: 10,810
  • Joined: 12-December 12

Re: Advice on techniques for writing code (my first semester)

Posted 13 October 2013 - 02:31 PM

Perhaps the OP has another (main) Class that he hasn't shown us ;)
Was This Post Helpful? 0
  • +
  • -

#15 jphoc13  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 166
  • Joined: 08-July 13

Re: Advice on techniques for writing code (my first semester)

Posted 13 October 2013 - 02:36 PM

I am going off a text book example and in the example there is no public static main void, here is the code I have built so far and only getting an error of not assigning a value to my variables.


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package employee;

/**
 *
 * @author Derek
 */
public class Employee 
{
    // Class with fields
    private final String position;
    private final String name;
    private final String department;
    private final int idNumber;

    // Constructor that accepts values as arguments and assigns
    public Employee(String employeeName, int idNum, String dpt,
            String pos) 
    {
        name = employeeName;
        idNumber = idNum;
        department = dpt;
        position = pos;

    }
    // setting mutators
    public void setPosition(String pos)
    {
        position = pos;
    }
    
    public void setName(String employeeName)
    {
        name = employeeName;
        
    }
    
    public void setDepartment(String dpt)
    {
        department = dpt
    }
    
    public void setIDNumber(int idNum)
    {
        idNumber = idNum
    }
    
    // setting accessors
    public String getPosition()
    {
            return position;
    }
}



Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2