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 2 of 2

Thread: What's wrong with my code?

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    My Mood
    Yeehaw
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question What's wrong with my code?

    public class ScienceFairProject
    {
    	// these are the  instance variables/
    private static String projectName;
    private static String contestantName;
    private int score; 
    static int projectA = 0; static int projectB=0; static int projectC=0; static int projectD=0;
     
    public static String getProjectName()
    	{
    		return projectName;
    	}
    public static String getContestantName()
    	{
    		return contestantName;
    	}
    public int getScore()
    	{
    		return score;
    	}
    /**
     * Precondition: score starts with zero
     * Postcondition: score will keep track of how many of each score the person got
     */
    public void setAddScore()
    	{
         if (score<2)
     
        	 projectA++;
     
         else if ((score>1)&&(score<3))
     
        	 projectB++;
     
         else if ((score>2)&&(score<4))
     
        	 projectC++;
     
         else
        	 projectD++;
    	}
    /**
     * Precondition: each project must have a score
     * Postcondition: will find the score most given
     */
    public static int getAverage()
    {
    	if (projectA>projectB)
     
    		return 1;
     
    	else if (projectB>projectC)
     
    		return 2;
     
    	else if (projectC>projectD)
     
    		return 3;
     
    	else
     
    		return 4;
     
    }
    public void setProjectName(String newProjectName)
    	{
    		projectName=newProjectName;
    	}
    public void setContestantName(String newContestantName)
    	{
    		contestantName=newContestantName;
    	}
    public void getScore(int newScore)
    	{
    		score=newScore;
    	}
     
    }


    public class ScienceFairProjectMain
    {
     
    public static void main (String[] args)
    	{
    	//project scores have been entered//
    	ScienceFairProject project1 =new ScienceFairProject();
    	project1.setProjectName("Earth");
    	project1.setContestantName("Ben");
    	project1.getScore(1);
     
    	ScienceFairProject project2 =new ScienceFairProject();
    	project2.setProjectName("Earth");
    	project2.setContestantName("Ben");
    	project2.getScore(1);	
     
    	ScienceFairProject project3 =new ScienceFairProject();
    	project3.setProjectName("Earth");
    	project3.setContestantName("Ben");
    	project3.getScore(1);
     
    	ScienceFairProject project4 =new ScienceFairProject();
    	project4.setProjectName("Earth");
    	project4.setContestantName("Ben");
    	project4.getScore(1);
     
    	ScienceFairProject project5 =new ScienceFairProject();
    	project5.setProjectName("Earth");
    	project5.setContestantName("Ben");
    	project5.getScore(1);
     
    	ScienceFairProject project6 =new ScienceFairProject();
    	project6.setProjectName("Earth");
    	project6.setContestantName("Ben");
    	project6.getScore(1);
    	/**
    	 * Precondition: will take all information
    	 * Postcondition: the project name, contestant and the average score will be written on the screen
    	 */
     
    	System.out.println("Project Name: "+ScienceFairProject.getProjectName());
    	System.out.println("Contestant Name: "+ScienceFairProject.getContestantName());
    	System.out.println("The average score is "+ScienceFairProject.getAverage());
    	}
     
     
    }

    My out put is OK for the Project & Contestant Name, but the Average Score is not calculating correctly. it keeps returning 4.


  2. #2
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: What's wrong with my code?

    Write some getters for Project1 .. Project n
    Print out the values before you call the average. It will make sense as to why you are getting 4 every time.

    Step through your code and follow the path it takes.

    Also, the formatting was lost on your first class, or it was done incorrectly, makes it terribly hard to read.

Similar Threads

  1. What is wrong with my code
    By chaffee in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 18th, 2013, 06:39 AM
  2. what is wrong with my code
    By BLUEJ in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 10th, 2013, 07:52 PM
  3. what is wrong with my code???
    By koolestkid20 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 25th, 2012, 01:12 PM
  4. What Am I doing Wrong In This Code?
    By RadiusUnknown in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 14th, 2012, 04:00 PM
  5. what's wrong in this code
    By Aravind in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 4th, 2011, 03:38 AM

Tags for this Thread