Results 1 to 10 of 10
  1. #1
    phixion is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default Help with program design.

    I just completed a Java I course in school. Since they aren't offering the second course until next Spring, I am trying to continue and further my studies on my own.

    I am trying to create a grading program. I started writing various methods, but I stopped and asked myself: would creating an object be better suited for this problem?

    I started to rethink the program and here is where I am at:

    • Prompt the professor to enter the number of graded homework assignments, tests, labs, etc. Allow him to enter the possible point total for each. (65 points for each homework assignment, 100 for each test, etc.) This information should be stored so that one doesn't have to reenter that information for each student. With that information, the total number of points possible for ALL assignments should be calculated.
    • Given the numbers he enters, prompt him to enter the grades for x number of homework assignments, x number of tests, labs, etc. (Homework 1: ___, Homework 2: ___, Test 1: ___, etc.)
    • Once completed, the information (weighted grade for homework, tests, etc., final grade, letter grade, etc.) should save to a file along with the student name/id number.
    • **Possibility: Allow the user to run a program, entering their name/student id, and printing their grades onto the screen.


    What is the best way to approach this problem? I am trying to make it as "modular" as possible.

    Looking over what I want to accomplish, would an object make more sense than solely writing methods to handle these problems? I mean, it IS an object oriented language and I need to start thinking of how to take advantage of the OO concept. I am not too comfortable with them as we only created very very basic objects.

    ANY insight would be helpful.

    Thanks in advance.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can make multiple objects for this, however; it will definitely be helpful to create a class which represents an individual student. This class can do all the manipulation you want(calculate grade, hold final grade, add more grades, change weights, Etc). Try and plan it out first. Start small and test one thing at a time, first work on the student class(or any other class you may want to use instead of student). It may be helpful to have a main method so you can continually compile and test as you go.

  3. #3
    phixion is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    I've stared at my computer screen for over 90 minutes with no progress. I'm realizing that aside from syntax, I didn't learn much in my class.

    When thinking about the program I want to write, I am confident that I can write the methods that will handle all of the manipulation. My problem lies in designing constructors, proper modifying methods (getter/setter methods I assume?), etc. I can write them, but I'm asking myself if I really understand what is going on.

    Maybe I can't learn by reading static text on a page, but reading tutorials gives me a headache. Often, they reference things I haven't been exposed to ('this' keyword, for instance), or the syntax appears so different, I quickly just scan to find anything I recognize. I need to figure something out while the desire to learn is still strong.

    And to think, I got a 99/100 in my class.

    Eh, sorry for venting.

    Mods, lock/delete this thread.

    I really want to learn Java so... back to square one!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think you/me/we learn best by doing. If you start writing hundreds of these things, they start to get second nature to you. Don't give up, but rather crank it up. YMMV.

    Luck!

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by Fubarable View Post
    I think you/me/we learn best by doing. If you start writing hundreds of these things, they start to get second nature to you. Don't give up, but rather crank it up. YMMV.

    Luck!
    YMMV?

    @op: I agree with fubar entirely. Don't give up, just think small. Since this project is a hard one for you dont get too overwhelmed. Think about what you need, and then just start at the smallest possible level.

    For instance, first work on just making a student class with a constructor and some instance variables, then create an instance of it in main.

    Once you have that add some getters, use the getters to print some info about the created class. If you run into problems, ask here.

    For instance, the use of the "this" keyword can be ignored in constructors and setters almost always. They really add a convenience. You should always try to have meaningful variable names, using "this" allows you to not create a new variable name.

    Java Code:
    public class X
      private int y;
      public X(int y){
        this.y = y;
      }
    }
    In this snippet there are two variables named y, one local to the constructor(it only exists in the constructor) and one as an instance variable(thus available anywhere in the class). But how can you differentiate between the instance variable and the local variable in the constructor? You use the this keyword. This refers to the class you are working in, so "this.y" represents the instance variable, and the y on the other side of the = is the local constructor variable.

    It's analogous to the fact that humans use last names to differentiate. It's possible for two people to have the same first name, how would they know which one you are calling if you simply refer to them by first name? They wouldn't. So you use a last name to help convey which one you want.

    A sloppier(IMO) approach to a constructor looks like this
    Java Code:
    public class X{
      private int y;
      public X(int newY){
        y = newY;
      }
    }
    Both snippets are the same. One uses this, one uses a different variable name.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  7. #7
    phixion is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    Thanks for the responses.

    In class, my concern didn't end with whether or not the program worked, I was always curious as to if I did things the right way, or correct way. Maybe I'm wrong, but just because something works, that doesn't mean it is correct.

    Looking at other beginner's code, I see things I have never been introduced to. That makes me question what I've learned thus far, if I'm doing things the correct way, etc.

    I'll get it together, ha. Working on the program at 3-4 in the morning didn't help, nor did starting over the the third time (I had a good portion of it completed but scrapped it. Maybe I can salvage some of that code later). I'll work on it some today and I'll post what I've come up with thus far.

    ** Thanks for the explanation of .this keyword. I understand it better now and will try to implement it.

  8. #8
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    I'll tell you what I did to start programming Java (still in this process) and have helped me a lot. O'Reilly has a great series of book called head first. Get Head First Java, Head First Object Oriented Analysis and Design and then Head First Design Patters. The first teaches you Java as a language and helps you overcome common problems. Learns you how to think OO. The second one, which is the one I loved, learn you how to take a description of a program and turn it into code. I'm reading Head First Design Patters right now which shows you ways to overcome difficult problems with designs which have already been used and tested for years. Trust me on those books they are wonderful.

    Now for your problem, you have to start thinking everything is an object and that objects work with each other. I'll do a rough description of how I think your program should be. It may not be the best way to do it since I'm no expert and you may not know some things yet.
    • You need a student class.
    • You need a professor class.
    • You need a module class.
    • You need an abstract testType class.
    • You need all the types of tests classes.

    Student class:
    instance variables: String name, int(or String if it has chars) id, ArrayList<Module> (or hashmap if you want better control but a bit more confusing for start) modules
    constructor: requires a name and an id to make a new student object
    get/set methods for instance variables
    Professor class:
    instance variables: String name, int/String profId
    constructor: requires a name and an id to make a new prof object
    get/set methods for instance variables
    Module class:
    instance variables: String name, int/String moduleId, String description, Professor prof etc
    Abstract TestType class:
    instance variables: String/enum type, int maxPoints, int weight
    Homework class extends TestType: etc etc |
    FinalExam class extends TestType: etc etc |

    You should work on your main like this:
    Create a student. Create a new module for the student. Add a graded test to the student for that module. Something like that... Haven't thought it through but you get the idea...

    Can't get to any more detail cause I have to leave right now. Work with this and give some feedback so we can help...

  9. #9
    phixion is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    I have only had a few minutes to work on this today. Here is my Student class.. thus far:

    Java Code:
    public class Student
    {
        private String studentName;  //instance variables
        private int studentID;
    
        public Student()  //default constructor
        {
            studentName = null;
            studentID = 0;
        }
        
        public Student(String studentName, int studentID)  //another constructor
        {
            this.studentName = studentName;
            this.studentID = studentID;
        }
        
        public Student(String studentName)  //another constructor
        {
            this.studentName = studentName;
        }
        
        public Student(int studentID)  //another constructor
        {
            this.studentID = studentID;
        }
        
        public void setInfo(String studentName, int studentID)  //modifying method
        {
            this.studentName = studentName;
            this.studentID = studentID;
        }
        
        public void setName(String studentName)  //modifying method
        {
            this.studentName = studentName;
        }
        
        public void setID(int studentID)  //modifying method
        {
            this.studentID = studentID;
        }
        
        public String getName()
        {   
            return studentName;
        }
        
        public int getID()
        {
            return studentID;
        }
    }

    I am hoping that looks okay; this is my first time using the .this keyword. It looks a little confusing (to me) to continuously use the same variable name, but I think I'm understanding it better.

    Here is a tester program to make sure it works:

    Java Code:
    import java.util.Scanner;
    
    public class StudentTester
    {
        public static void main (String[] args)
        {
            Scanner keyboard = new Scanner(System.in);
            
            System.out.print("Student Name: ");
            String studentName = keyboard.nextLine();
            
            System.out.print("Student ID: ");
            int studentID = keyboard.nextInt();
            
            Student studentInfo = new Student(studentName, studentID);  //new student object, passing studentName/studentID as arguments
            
            System.out.println(studentInfo.getName());  //accessing getName/getID methods
            System.out.println(studentInfo.getID());
        }
    }

    I ask myself why can't I just break up the main method and have separate methods for obtaining the student name and student id. In other words, have a method that prompts the user for the student name, and returns that value. It seems as though it would eliminate the need for a Student class. What am I not understanding?

    Here is an example:

    Java Code:
        public static String studentName()
        {
            Scanner keyboard = new Scanner(System.in);
            
            System.out.print("Student Name: ");
            String studentName = keyboard.nextLine();
            
            return studentName;        
        }
    Java Code:
        public static void main (String[] args)
        {        
            System.out.print(studentName());   
        }

    I apologize for the elementary questions. I know this stuff better than I think, but I'm off to a slow start and am just second guessing everything I've learned thus far.

  10. #10
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    In general I guess you don't want just a class that prints the name of A student but for any student that you may want to add later. Making a student class you can instantiate as many students as you like. They can have their own modules, tests and any data you may add to them and you can use any of those data to do calculations etc. How are you going to bind all that data without various classes. How will you know what modules student1 has or student2 has? What tests they have taken? What professor each module has etc? Building the proper classes all that data starts to make sense.

    In my opinion you should delete the empty constructor for sure. Do you really want a student object if you don't know what the students name is or at least what his ID is? I would suggest having only the version that takes both data. If you want to use multiple constructor, it's a common practice you don't repeat code. So you could do something like:
    Java Code:
    public constructor (int number){
        this.number = number;
    }
    
    public constructor () {
        this(0);
    }
    So in this case you only need to edit the first constructor if for instance you need to add a data verification or something:
    Java Code:
    public constructor (int number){
        if (number >= 0) {
          this.number = number;
       } else {
          this.number = 0;
       }
    }
    When you start using object you can do all kinds of stuff. For instance you could add a toString() method to your Student class like this:
    Java Code:
    public String toString(){
       return "Student name: " + studentName + " (ID: " + studentID + ")";
    }
    So now you could make a new student object and and just go System.out.println(student1); and it will print the whole sentence (not sure if it needs student1.toString()).

    Using .this keyword is right when you are referring to an instance variable but it's not needed if you don't have a local variable with the same name.

    You generally use the main method to initiallize you app. For testing purposes you don't have to make another method for input. You could do it thought if you are taking a lot of user input and you don't want to repeat code:
    Java Code:
    public String userInput(String question){
        Scanner keyboard = new Scanner(System.in);
        System.out.print(question + ": ");
        String output= keyboard.nextLine();
        return output;
    }
    So now you have a general purpose userInput method that you could call:
    Java Code:
    Student1.setName(userInput("Give students name");
    Student1.setID(userInput("Give students ID").toInteger);
    Keep writing the other classes and you'll get the drift :)
    Last edited by santeron; 05-16-2011 at 03:45 AM.

Similar Threads

  1. Replies: 2
    Last Post: 03-03-2011, 09:23 AM
  2. Program design
    By phil128 in forum New To Java
    Replies: 0
    Last Post: 01-24-2011, 06:48 PM
  3. GUI design
    By kennyblue in forum AWT / Swing
    Replies: 6
    Last Post: 11-10-2010, 06:59 PM
  4. JSP Design.
    By makpandian in forum NetBeans
    Replies: 0
    Last Post: 04-20-2009, 02:21 PM
  5. Help design a program - multithread?
    By rushenas in forum New To Java
    Replies: 4
    Last Post: 01-23-2009, 07:41 AM

Posting Permissions

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