954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java Objects and Classes

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.

ZeroEddy
Newbie Poster
16 posts since Oct 2011
Reputation Points: 10
Solved Threads: 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.

tkud
Posting Whiz in Training
235 posts since Sep 2009
Reputation Points: 13
Solved Threads: 46
 

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 .

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

And you can always start with the basic tutorial:
http://download.oracle.com/javase/tutorial/java/concepts/index.html

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

ZeroEddy
Newbie Poster
16 posts since Oct 2011
Reputation Points: 10
Solved Threads: 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;
    }
}
ZeroEddy
Newbie Poster
16 posts since Oct 2011
Reputation Points: 10
Solved Threads: 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???

Typically when someone writes an abstract class, the prefixget 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.");    
    	       }
       }
    }
}
Anyday
Newbie Poster
15 posts since Oct 2011
Reputation Points: 11
Solved Threads: 2
 

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.

ZeroEddy
Newbie Poster
16 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: