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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: Need help creating this program

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help creating this program

    K, so I have to convert this problem into a method:

    5) Write a method named attendance that accepts a number of lectures attended by a student, and returns how many points a student receives for attendance. The student receives 2 points for each of the first 5 lectures and 1 point for each subsequent lecture.

    Right now I have this algorithm:
    //prints points received if the student attends 7 lectures
    //parameter is how many lectures he attended
    //for first 5, he receives 2 points
    //for next 2 he receives 1 point

    I just don't know how to convert it into code. Can one of you guys help me please?


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Need help creating this program

    Hello ixjaybeexi.

    This can be a starting block for you:

    import java.util.Scanner;
     
    public class StudentAttendance {
     
        /**
         * JavaProgrammingForums.com
         */    
        static int lectures, points;
     
        // attendance method
        public static void attendance(){
     
     
        }
     
        public static void main(String[] args) {
     
            Scanner sc = new Scanner(System.in);
            System.out.println("How many lectures has this student been to?");
            lectures = sc.nextInt();
            attendance();
     
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help creating this program

    we don't have to have the user input the amount of lectures attended, we just have to make up a number, and in my case it's 7. i know he's supposed to get 12 points, i just don't know the math behind it.

  4. #4
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help creating this program

    can anyone help me?

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help creating this program

    Write your algorithm out for if he attends n lectures. If it's always going to be 7 lectures, you might as well do this:

    System.out.println("For 7 lectures you get 12 points");

    Even if you're test case is 7, they probably want it to work for any arbitrary n.

  6. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help creating this program

    Well what I have so far is this

    public class Lab6 {
         public static void main (String[] args){
                   System.out.println("The amount of points received are " + attendence(7));
         }
     
    public static int attendence (int lecturesAttended){

    As for the algorithm, yes it has to be in terms of n. I just don't know what math to do...do I divide by 5 then multiply by 2? I have no idea and I've been trying to do this for 2 days now

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help creating this program

    what is the algorithm for n lectures attended?

    first 5 give 2 points, all rest give 1?

    pseudo-code:
    count = lecturesAttended
    while count > 0
         if count > 5
              increase points by 1
         else
              increase points by 2
         decrease count
    end

  8. #8
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help creating this program

    I have no idea what your pseudo code means...we didn't learn how to do if and else statements yet

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help creating this program

    what do you know how to do? From the way you phrased the question, it seems like you really do need some way to test conditionals, and if/else are the simplest.

    if/else is basically like it sounds like:

    if it's sunny, go play outside.
    else, play video games.

  10. #10
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help creating this program

    that's why i'm asking you guys hahaha, i don't know how to answer the question. i could use if/else if you could teach me how to do it...?

  11. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help creating this program

    if/else statements are really simple. You ask is something true? If it is, you execute one set of code. If it's not, you execute the else code. It's almost exactly like the language equivalent: if something, else something different.

    if (conditional)
    {
        do something;
    }
    else
    {
         do something else;
    }

    here's what an if/else statement looks like in code. The conditional is the thing you want to test, and if it evaluates to true, do what ever is surrounded by the first {}'s. If it's false, do what ever is surrounded by the second {}'s.

  12. #12
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help creating this program

    Hmm, alright.
    Could I use this algorithm?

    1) In terms of n, for each lecture attended, student gets 2 points for the first 5 and 1 for there after
    -If lecturesAttended > 5, points = lecturesAttended + 2
    -Else lecturesAttended < 5, points = lecturesAttended + 1
    2) return points

    Or would that not work at all?

  13. #13
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help creating this program

    Mmm, there's a few problems but they can be easily fixed.

    You don't want to add 1 or 2 to the lecturesAttended to get the points, but rather multiply. You also have it backwards. It should be 2 points/lecture for the first 5 and 1 point after. Also, you still need to add the 5 points for the first 5 lectures, and then (I think you can work out why, if not please ask)

  14. #14
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help creating this program

    Alright so basically, it should be

    -if lectures <= 5, points = lectures * 2 + 5
    -else lectures > 5, points = lectures * 1

    Is it possible to do like points = points + points? That just looks so ridiculous, but i know I have to add the two from the if statement, and the else statement to return the final points...or should i just do something like this:

    -if lectures <= 5, points1 = lectures * 2 + 5
    -else lectures > 5, points2 = lectures * 1
    -finalPoints = points1 + points2
    -return finalPoints

  15. #15
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help creating this program

    an if/else statement only does one or the other... not both. Also, you want to add the 5 points if the lectures is greater than 5.

    Yes, you can do points = points+points, but it's not helpful here.

  16. #16
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help creating this program

    Ya lost me, I'm kinda confused now.

  17. #17
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help creating this program

    if lectures <= 5
    points = 2 * lectures
    else
    points = 5 + lectures

  18. #18
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help creating this program

    OH, wow alright I think I get it now, haha, that was a lot simpler than I thought, and then I just return points back into my main..

  19. #19
    Junior Member
    Join Date
    Oct 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help creating this program

    Ok, so here's my code:

    public static int attendence (int lecturesAttended){
    	if lecturesAttended <= 5 {
    		int points = 2 * lecturesAttended;
    	}
    	else {
    		points = 5 + lecturesAttended;
    	}
    	return points;
    }

    And when I go to compile it, it gives me a whole bunch o' errors...

  20. #20
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help creating this program

    it's because of variable scope. I don't know a good way to explain it, but basically you're declaring the int inside the if statement, so nothing outside of it can see it.

    The last problem I see is that you forgot to put parenthesis around the conditions for the if statement.

    public static int attendance(int lecturesAttended)
    {
         int points;
         if (lecturesAttended <= 5)
         {
              points = 2*lecturesAttended;
         }
         else
         {
              points = 5 + lecturesAttended;
         }
         return points;
    }

  21. #21
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Need help creating this program

    here's a full 1 i came up with while reading that ,idk about grammar spelling or syntax but otherwise here it is

     
    public int attendance (int a){
        if (a >= 5){  // checks if the times he attended is 5 or more 
          points = 10;   // if it is he gets the 10 points for each of the 1st 5 lectures
          a = a - 5;          // then while subtract 5 from the times he attended
           while (a > 0) {    // and for every time after that he gets 1 more point 
              points = points + 1;   // adds the point
              a = a - 1;  // subtracts 1 from the attendant before the loop runs again 
           }                  // so that he wouldn't get free points  
       } else {
           while (a > 0) {   // if he hasn't attended the first 5 lectures then he gains 2 points for each he has 
              points = points + 2;
              a = a - 1;     // essentially the same as the previous over 5 point loop but gives 2 points instead 
           }   
       return points;
    }

    look about right?
    Programming: the art that fights back

  22. #22
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help creating this program

    Yep, that's right. There's only one bit of syntax error. You forgot to declare and initialize points to 0.

    In terms of performance, my solution is better ( O(1) vs. O(n) )

  23. #23
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Need help creating this program

    after i posted and read back through it i realized , i was just too lazy to fix it XP
    Programming: the art that fights back

  24. #24
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Need help creating this program

    is this post already solved?


    public class Attendance {
     
        private int totalPoints;
     
        public Attendance(int lecturesAttended) {
     
            setPoints(lecturesAttended);
        }
     
        public void setPoints(int lecturesAttended) {
     
            int attendance,
                firstFive= 0;
     
            int excess;
     
            attendance = lecturesAttended;
     
            if (attendance >= 5) {
     
                firstFive= 2;
     
                excess = attendance - 5;
     
                totalPoints = (excess * 1) + firstFive;
            }
            else {
     
                System.out.println("No Points Recieved!");
            }
        }
     
        public int getPoints() {
     
            return totalPoints;
        }
     
        //main method to test if the defined method works
        public static void main(String[] args) {
     
            Attendance att = new Attendance(7);
     
            att.setPoints(5);
     
            int points = att.getPoints();
     
            System.out.println(points);
        }
    }

    is this supposed to be like that? this is what i understand on the case of ijaybeexy..

    for first five = recieve two points,
    the rest 1 point


    so if the lectures attended is 7 then the total points will be 4. right?

  25. #25
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Need help creating this program

    i expanded mine so its more understandable
    and added it in a class with main for testing

    i think it was 2 points for each of the first 5 ( aka 1a = 2p , or 2a = 4p)
    then if its over 5 they get 1 more for each ( aka 6a = 11p , or 9a = 14p)
    ( a = attendance , p = points )

    public class Attendance {
     
    	public static void main(String[] args) {
     
    		int response = attendance(4);
     
    		System.out
    				.println("some 1 attended four times , witch means his score should be 8. it is "
    						+ response);
     
    		response = attendance(5);
     
    		System.out
    				.println("some 1 attended five times , witch means his score should be 10. it is "
    						+ response);
     
    		response = attendance(6);
     
    		System.out
    				.println("some 1 attended six times , witch means his score should be 11. it is "
    						+ response);
     
    		System.out
    				.println("this should test all three run troughs of the method ");
    	}
     
    	public static int attendance(int attend) {
     
    		int points = 0;
     
    		if (attend >= 5) {                                  // checks if the times he attended is 5 or more
     
    			points = 10;                                   // if it is he gets the 10 points for each of the 1st 5
    							                       // lectures
     
    			attend = attend - 5;                      // then while subtract 5 from the times he
    									       // attended
     
    			while (attend > 0) {                       // and for every time after that he gets 1 more
    									       // point
     
    				points = points + 1;               // adds the point
     
    				attend = attend - 1;               // subtracts 1 from the attendant before
    										// the loop runs again
    			}                                                      // so that he wouldn't get free points
     
    		} else {
     
    			while (attend > 0) {                // if he hasn't attended the first 5 lectures
    									// then he gains 2 points for each he has
     
    				points = points + 2;
     
    				attend = attend - 1;               // essentially the same as the previous
    										// over 5 point loop but gives 2 points
    										// instead
     
    			}
     
    		}
    		return points;
    	}
    }
    Last edited by wolfgar; October 18th, 2009 at 10:50 PM.
    Programming: the art that fights back

Page 1 of 2 12 LastLast

Similar Threads

  1. Creating variables from a text file
    By Larz0rz in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 4th, 2009, 11:57 PM
  2. creating a gui
    By rsala004 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 21st, 2009, 02:17 AM
  3. Creating A Count Matrix In Java
    By statsman5 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 14th, 2009, 04:40 PM
  4. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM