0

Hey,
I have been studying Java for a year in college and this year we started Object Oriented Programming. We have been givin some assignments and stuff but they all include creating Objects and Classes. Does anyone know of any good resources for Creating these as I am slowing down and getting in over my head trying to create them.
Any help will do wonders cheers.

5
Contributors
7
Replies
9
Views
6 Years
Discussion Span
Last Post by ZeroEddy
0

This is funny.. How can you claim to have studied Java for a year and you don't know how to create objects and classes? Nobody would believe that.

0

tkud has a point, Objects and Classes are very basic stuff in an Object Oriented language.

anyway, a good way to start, would be to take a look at the sticky thread on top of this forum. Or, you can visit it by clicking here.

0

Thank you for your feedback. Its just I am getting confused when I am trying to create the constructor and passing arguments to it etc. Anyway thanks for your replys

0

Below is my code which I have written in an attempt to create the object/class for a student grade calculator. I am experiencing errors. Am I heading in the right track does anyone know???


****The main class****

public class StudentMain
{
    public static void main (String[] args)
    {
        Student stdGrade = new Student("marks");

        
        stdGrade.getMarks();
        
        stdGrade.setGrade();
    }
}

****The Student class****

import javax.swing.*;

public class Student
{
    private int array[] = new int [20];
    private int mark;
    private String marks;
    
    
    //constructor
    public Student(String stdMarks)
    {
       marks = stdMarks;
    }
    
    //get marks method
    public void getMarks()
    {
        for(int i=0;i<array.length;i++)
        {
            marks = JOptionPane.showInputDialog("Please enter marks for student " +(i+1));
            array[i] = Integer.parseInt(marks);
            
            while(array[i] < 0 || array[i] > 100)
            {
                JOptionPane.showMessageDialog(null, "Marks entered must be between 0-100. Please re-enter");
                marks = JOptionPane.showInputDialog("Please enter marks for student" +(i+1));
                array[i] = Integer.parseInt(marks);
            }
        }
    
    }
    
    //set grade method
    public static int setGrade(int array2[])
    {
           for (int i=0; i<array2.length;i++)
        {
          if( array2[ i ] >= 85)
                {
                   
                    System.out.println("Student " +(i+1) + " recieved " +array2[i]+ " marks. Overall result is a Distinction."); 
                }//end if
                else if( array2[ i ] > 65 && array2[ i ] <85)
                {
                    System.out.println("Student " +(i+1) + " recieved " +array2[i]+ " marks. Overall result is a Merit."); 
                }//end if
                else if( array2[ i ] >= 40 && array2[ i ] <65)
                {
                    System.out.println("Student " +(i+1) + " recieved " +array2[i]+ " marks. Overall result is a Pass."); 
                }//end if
                else
                    System.out.println("Student " +(i+1) + " recieved " +array2[i]+ " marks. Overall result is a Fail.");    
        }
        return int;
    }
}

Edited by Ezzaral: Fixed code tag.

1

Below is my code which I have written in an attempt to create the object/class for a student grade calculator. I am experiencing errors. Am I heading in the right track does anyone know???

Typically when someone writes an abstract class, the prefix get is what returns something, and the prefix set usually returns void (or boolean for success or failure). Your methods were backwards -- I fixed them for you, but I suggest you look it over and understand what you did wrong. You also had a method as 'static,' which is incorrect for an abstract class as an abstract class is just a definition for what an object is, while 'static' implies specificity.

By the way, personal gripe of mine: When you write if-statements, write the else-if's on the same indentation so it's easier to follow.

import javax.swing.JOptionPane;

public class Student
{
    private int array[] = new int [20];
    private String marks;
    
    
    //constructor
    public Student(String stdMarks)
    {
       marks = stdMarks;
    }
    
    //get marks method
    public void setMarks()
    {
        for(int i=0;i<array.length;i++)
        {
            marks = JOptionPane.showInputDialog("Please enter marks for student " +(i+1));
            array[i] = Integer.parseInt(marks);
            
            while(array[i] < 0 || array[i] > 100)
            {
                JOptionPane.showMessageDialog(null, "Marks entered must be between 0-100. Please re-enter");
                marks = JOptionPane.showInputDialog("Please enter marks for student" +(i+1));
                array[i] = Integer.parseInt(marks);
            }
        }
    
    }
    
    //set grade method
    public void getGrade()
    {
    	for (int i=0; i<array.length;i++)
    	{
    	       if( array[ i ] >= 85)
    	       {
    	                
    	           System.out.println("Student " +(i+1) + " recieved " +array[i]+ " marks. Overall result is a Distinction."); 
    	       }//end if
    	       else if( array[ i ] > 65 && array[ i ] <85)
    	       {
    	           System.out.println("Student " +(i+1) + " recieved " +array[i]+ " marks. Overall result is a Merit."); 
    	       }//end if
    	       else if( array[ i ] >= 40 && array[ i ] <65)
    	       {
    	           System.out.println("Student " +(i+1) + " recieved " +array[i]+ " marks. Overall result is a Pass."); 
    	       }//end if
    	       else
    	       {
    	     	  System.out.println("Student " +(i+1) + " recieved " +array[i]+ " marks. Overall result is a Fail.");    
    	       }
       }
    }
}

Edited by Anyday: n/a

Votes + Comments
Very Helpful
0

Thank you so much Anyday for your reply. And I will take your word and revise over it to see my faults. Now that I have one program running I can refer back to it. Thank you so much.

Edmond.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.