Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Why output is displaying 0 for calculation in java?

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Why output is displaying 0 for calculation in java?

    I'm almost done with my assignment but i have a small problem. I need to calculate the value for Class Standing "CS" here's my three classes.

    The only problem is when i run the Outcome, the output for Class Standing is 0 what should i add to my script? thanks

    Java Code
    public class Outcome
    {
    	public static void main(String[]args)
    	{
    		Student s=new Student();
    		grade g=new grade();
     
    		s.setQuiz(1);
    		s.setAssignment(2);
    		s.setMachineProblem(3);
    		s.setSeatWork(4);
    		s.setName("Charles");
    		s.setStudNumber(510180);
    		s.setYrLevel(1);
     
    		System.out.println("Name: "+s.Name);
    		System.out.println("Year Level: "+s.YrLevel);
    		System.out.println("Stud Num: "+s.StudNumber);
    		System.out.print("Class Standing: "+g.CS);
     
    	}
    }

    Java Code
    public class grade
    {
     
    	double CS;
     
    		public void CS(double Quiz, double Assignment, double SeatWork, double MachineProblem)
    		{	
    			CS=(Quiz+Assignment+SeatWork+MachineProblem);
    		}
     
    		public double getCS()
    		{
    			return CS;
    		}
    }

    Java Code
     
    public class Student
    {
     
    String classword2;
    String Name,Course;
    int StudNumber,YrLevel;
    double Quiz,Assignment,SeatWork,MachineProblem,ExamGrade;
     
    	public void setName(String inputword1)
    	{
    	Name=inputword1;
    	}
    	public String getName()
    	{
    	return Name;
    	}
    		public void setStudNumber(int inputword2)
    		{
    		StudNumber=inputword2;
    		}
    		public int getStudNumber()
    		{
    		return StudNumber;
    		}
    			public void setYrLevel(int inputword3)
    			{
    			YrLevel=inputword3;
    			}
    			public int getYrLevel()
    			{
    			return YrLevel;
    			}
    				public void setCourse(String inputword4)
    				{
    				Course=inputword4;
    				}		
    				public String getCourse()
    				{
    				return Course;
    				}
    					public void setQuiz(double inputword4)
    					{
    					Quiz=inputword4;
    					}		
    					public double getQuiz()
    					{
    					return Quiz;
    					}
    						public void setAssignment(double inputword6)
    						{		
    						Assignment=inputword6;
    						}
    						public double getAssignment()
    						{
    						return Assignment;
    						}
    							public void setSeatWork(double inputword7)
    							{
    							SeatWork=inputword7;
    							}
    							public double getSeatWork()
    							{
    							return SeatWork;
    							}
    								public void setMachineProblem(double inputword8)
    								{
    								MachineProblem=inputword8;
    								}
    								public double getMachineProblem()
    								{
    								return MachineProblem;
    								}
    									public void setExamGrade(double inputword9)
    									{
    									ExamGrade=inputword9;
    									}
    									public double getExamGrade()
    									{
    									return ExamGrade;
    									}
    }
    Last edited by Deep_4; November 7th, 2012 at 01:44 PM.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Computing Classes

    you never call grade.CS(...) and set the value of CS within the grade class, so when you try to read the value of CS it is 0. If this asn't java you would have just got garbage output because at no point did you initialize CS within your grade class.

    Chris

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Computing Classes

    my teacher told me not to assign any value in grade class, all the initialization for all the variables must be in the outcome class
    Last edited by tazjaime; April 26th, 2009 at 06:19 AM.

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Computing Classes

    Thats fine, but like I told you. You havent done that either.

    Chris

Similar Threads

  1. Replies: 6
    Last Post: May 15th, 2009, 05:06 PM
  2. [SOLVED] How to link two different class?
    By John in forum Object Oriented Programming
    Replies: 11
    Last Post: April 27th, 2009, 02:57 PM
  3. [SOLVED] Java program using two classes
    By AZBOY2000 in forum Object Oriented Programming
    Replies: 7
    Last Post: April 21st, 2009, 06:55 AM
  4. Problem in merging two java classes
    By madkris in forum Object Oriented Programming
    Replies: 11
    Last Post: March 16th, 2009, 09:02 AM
  5. Java program with abstract class along with two subclasses
    By crazydeo in forum Collections and Generics
    Replies: 2
    Last Post: June 10th, 2008, 11:45 AM