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

Thread: Efficent idea to use on this question...

  1. #1

    Default Efficent idea to use on this question...

    I'm aware I have posted several questions within the last 30 minutes and I do apologize for that. I'm just trying to learn this and grasp the concept of it. Now in this following class, I have five subjects with scores initizliaed to 40. I have one method that takes in arguments from the user. The other method that's a returning type returns the score. Rather, then having to write
    public int Pe Score()
    {return pe;}
    and
    public int sciencescore(){return science;}
    and same for the other 3 subjects. Is there another effiecnte way to write this, because writing this five times doesn't seem to be a smart method..i was just thinking of using a void and printint all of them out...

    public class StudentScore
    {
    	private int math;
    	private int science;
    	private int calc;
    	private int history;
    	private int pe;
     
    	//Make a constructor
    	public StudentScore()
    	{
    		math=40;
    		science=40;
    		calc=40;
    		history=40;
    		pe=40;
     
    	}
     
    	public void getscore(int m_score,int s_score,int c_score,int h_score,int P_score)
    	{
    		math=m_score;
    		science=s_score;
    		calc=c_score;
    		history=h_score;
    		pe=P_score;
     
     
    	}
     
    	public int Scores()
    	{
    		return math;
     
    	}
    	public int scienceScore()
    	{
    		return science;
    	}
    }


  2. #2
    Junior Member
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Efficent idea to use on this question...

    you can actually use hashmap
    write a function which returns hashmap
    store your each subject scores in hashmap<K,V> where K is subject name and value is subject score
    and in main function u can get the subject score associated with the subject name.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Efficent idea to use on this question...

    Or, each student object is an instance of a class Student that has methods to return the attributes of each Student object as they are needed. You didn't show what a 'subject' is, but I suspect they are each a loose collection of scores without form, not objects. Write a Student class, and come back if you need help.

Similar Threads

  1. Need help with making code more efficent! - PLEASE HELP:(
    By ProgrammablePeter in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 1st, 2013, 05:02 PM
  2. Almost got no idea how to do this! Need some H E L P!
    By maronski in forum Loops & Control Statements
    Replies: 6
    Last Post: July 30th, 2012, 10:23 PM
  3. Idea's on what else to add?
    By CodyReuille in forum Java Theory & Questions
    Replies: 2
    Last Post: October 14th, 2011, 04:34 PM
  4. need help with my idea!
    By frostwing in forum Object Oriented Programming
    Replies: 3
    Last Post: February 10th, 2011, 09:53 AM
  5. No idea what to do for this
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 18th, 2010, 07:16 PM