• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

My Java program is not adding my new feature age

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to add the feature "age" and I am getting 0 as average for each student. This is the problem of logic that got me quit Java for 2 days but today I am coming trying to figure it out without any success. Any help would be very appreciated. Thanks.

 
Bartender
Posts: 4691
183
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In line 52 you set total1 to zero, so the average will be zero in line 53. Your code lacks proper indentation, so it is very hard to see if all curly braces are correctly placed. Can you indent your code to make it much more readable?

Have you heard of integer-division?

Last remark: you have a variable tota1l in your student-loop, and another one later on. That is very error prone!
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You havent resolved numTests, total, or age to a variable type.
 
Piet Souris
Bartender
Posts: 4691
183
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Will,

Welcome to the Ranch and enjoy the stay!
 
Fred Masen
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the code again. I guess improved but I am still getting a 0 as age average.

 
Fred Masen
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok the program almost work. But how can I get the program giving me the final average age ? After running the program for example for 3 students age? What it does , is that it gives me the average of each student which is not what I am looking for. I want the program to ask me the age of the student then at the very end giving me the average of the total age of all students? Thanks.

 
Fred Masen
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried that but I am getting the same "average" than the average score. Please help. I am getting crazy.

 
Fred Masen
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each I quit Java programming for those kind of logic mistakes. This has been going on for years now. Please help me
 
Saloon Keeper
Posts: 1652
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two problems with the code as shown.

1. The indentation of your code means nothing to java, this is not Python, where the indentation changes the meaning of your lines of code to the interpreter.  It means everything to the reader, tho.  Part of your confusion is shared by other users because of inconsistent indentation obscuring the actual flow of logic.  You want to fix that.

2. What is the formula for computing the average age of all of your students?  How does it differ from that of computing the average test score for all of your students?  The naming of your variables is less confusing and mysterious than millions of variables that have been used in millions of programs in the past, but it is still vague enough to confuse you.  Specifically, "What is the exact definition of how you use total?"

This is frustrating, but the skills you learn right here are going to be useful thru an entire career of programming, even if that is decades long.  It is very important to write code that won't be confusing to others, even if you never show it to anyone, because then that code won't be confusing to YOU either.
 
Fred Masen
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok by guys. As I can see CodeRanch has changed. Before I used to get some help but you guys are now becoming like Stack Overflow and ignoring the problems of people who are having issues  At least not like the others I tried to solved this issue for 2 days before posting my post. Bye.
 
Fred Masen
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know  Silverman. My example is from a book from Gaddis called Starting out Java. The same issue will be true for any language. Bye. I will go back to play chess at least there I don't need help
 
Jesse Silverman
Saloon Keeper
Posts: 1652
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Masen wrote:Each I quit Java programming for those kind of logic mistakes. This has been going on for years now. Please help me



You may be very close to being fine each time.

Patience is important, almost everyone trying to help has probably spent more than ten hours trying to find one small bug in code written by others more than a few times (I certainly have!)

Your coding style is less confusing than many others we have had to work with, but was just confusing enough to confuse yourself here too.  That is definitely something that responds to reflection and practice.

In this case I think that if you had written out the exact steps you are planning to take in English/French before coding, you might not have the problem now.  As long as you don't give up prematurely, we will see.

Had you mentioned from which sources you have been trying to learn?

Tough guy programmers might like to pretend that all they ever needed was a Reference Manual, and they could code when they were 2 years old, but good beginning tutorials should help you thru the steps necessary to get even relatively simple logic working correctly.  Mediocre ones may not!
 
Fred Masen
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a Senior who tried to program to prevent memory loss and dementia.
 
Fred Masen
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will get back to my chess game instead. Bye all.
 
Bartender
Posts: 2899
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's start over. Leave the code aside.

Assume the below input:


What is your expected output ?
 
Fred Masen
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many students do you have? 3
How many test scores per student? 2
Enter the age of student 1
10
Student number 1
--------------------
Enter score 1: 20
Enter score 2: 30
The average for student 1 is 30.0.

And the average age is : 60.0
Enter the age of student 2
15
Student number 2
--------------------
Enter score 1: 40
Enter score 2: 50
The average for student 2 is 82.0.

And the average age is : 82.0
Enter the age of student 3


And the code is :

 
salvin francis
Bartender
Posts: 2899
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I suggested, leave the code aside.

you posted:


Is this your Expected output ?
 
Fred Masen
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The weird thing is that it is a simple program but I am still unable to solve it. I know by instinct that it is missing a for loop for the age. But I tried to include it yesterday with issues so I erased it.  I came 2 years ago and I was able to solve complicated programs than this one but still gave up. To sum up I gave up Java for the past 8 years . So I am frustrated. Even if I tried to use a different language I will get the same issue. It is a problem of logic. My brain is not functioning well.
 
salvin francis
Bartender
Posts: 2899
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree, but right now, the issue has nothing to do with java.
Let's communicate effectively, you're stuck at a point, we're trying to help you out.
 
Fred Masen
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Francis it is giving me the same average for the score and the age. That's the issue. therefore a for loop for age is needed. Burt where to stick it to the code without getting errors ?
The average for student 2 is 82.0.

And the average age is : 82.0
 
salvin francis
Bartender
Posts: 2899
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's break the problem into smaller parts:

Problem1:

I would expect the average score to be (20+30) / 2 = 25.

Problem2:

I would expect the average score to be (40+50)/2 = 45

Do you agree ?
 
Fred Masen
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes
 
salvin francis
Bartender
Posts: 2899
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perfect !!
Now, I've removed the age feature entirely from your code:


output:

I see that in the first round, it behaves correctly. The second time, score average is off. Are you able to spot why ?
 
salvin francis
Bartender
Posts: 2899
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hint: it's the variable called "total"
 
Marshal
Posts: 74381
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 42 calculates something called average as total / numTests. Please put spaces like that around your binary operators, to improve legibility. If you want the result to be a double, make the expression a double. What you have is an int expression and you are assigning it to a double variable. That means you will never have a fractional part in the result. Read the input as doubles, or cast the left operand to a double or something, to make the value to the right of thesign of type double.
You have several variables with similar names; average and average1 together are a sure‑fire recipe for confusion. It is possible that you are mistakenly using the same values to calculate the average mark as the average age. I don't understand lines 43 and 47. I don't know that you are dividng the right variables.
Only use \n or \r if somebody specifically tells you they want the LF or CR characters; otherwise for printf() use %n instead.
I suggest:-
  • Go through your code and make sure all your variables' names make their purpose clear: I might use, e.g. testCount, totalAges, totalMarks.
  • Decide which of those variables should be ints and which doubles.
  • Check that you are dividing the right variables for each mean.
  •  
    Fred Masen
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Francis what you have posted is what the book of Gaddis starting out Java example program was giving me and I understand it perfectly fine. In order to improve my coding skills I tried to included the age feature but it does not work because as I said it seems to be missing a for loop for the age feature. .
     
    salvin francis
    Bartender
    Posts: 2899
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Fred Masen wrote:Francis what you have posted is what the book of Gaddis starting out Java example program was giving me and I understand it perfectly fine. In order to improve my coding skills I tried to included the age feature but it does not work because as I said it seems to be missing a for loop for the age feature. .


    I understand it, but before adding "age", have a look at scores. The scores code is not working as well. In your code, the output comes to :

    50 is wrong. There's a reason why this is happening.
     
    Fred Masen
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Campbell here's what I get when I changed to double :
    What I would like from the program is to ask me the age of the student and at the very end giving me average of all the ages .

    How many students do you have? 2
    How many test scores per student? 2
    Enter the age of student 1
    26
    Student number 1
    --------------------
    Enter score 1: 140
    Enter score 2: 140
    The average for student 1 is 153.0.

    And the average age is : 306.0
    Enter the age of student 2
    28
    Student number 2
    --------------------
    Enter score 1: 250
    Enter score 2: 250
    The average for student 2 is 417.0.

    And the average age is : 417.0
     
    Fred Masen
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Francis here is what I get :
    How many students do you have? 2
    How many test scores per student? 2
    Enter the age of student 1
    15
    Student number 1
    --------------------
    Enter score 1: 10
    Enter score 2: 20
    The average for student 1 is 22.0.

    And the average age is : 45.0
    Enter the age of student 2
    10
    Student number 2
    --------------------
    Enter score 1: 30
    Enter score 2: 40
    The average for student 2 is 62.0.

    And the average age is : 62.0
     
    Campbell Ritchie
    Marshal
    Posts: 74381
    334
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Print all the intermediate variables at each run, too. You are only seeing the wrong results at present. You need to see any internal errors causing those incorrect results.
     
    Fred Masen
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Here's the Gaddis example from the book :
    And I want is to add an age feature that allows me to get the average age of the students. Bu I can't figure it out.



    Program Output with Example Input Shown in Bold
    How many students do you have? 3 [Enter]
    How many test scores per student? 3 [Enter]
    223224
    Chapter 4   Loops and Files
    Student number 1
    -----------------------
    Enter score 1: 100 [Enter]
    Enter score 2: 95 [Enter]
    Enter score 3: 90 [Enter]
    The average for student number 1 is 95.0.
    Student number 2
    ----------------------
    Enter score 1: 80 [Enter]
    Enter score 2: 81 [Enter]
    Enter score 3: 82 [Enter]
    The average for student number 2 is 81.0.
    Student number 3
    ----------------------
    Enter score 1: 75 [Enter]
    Enter score 2: 85 [Enter]
    Enter score 3: 80 [Enter]
    The average for student number 3 is 80.0.
     
    salvin francis
    Bartender
    Posts: 2899
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    I wrote:...

    50 is wrong. There's a reason why this is happening.



    Here's the answer to this. The average should have been


    But the behavior of the code is:

    Do you know what this 15 is ? Campbell's post has more clues about it.
     
    Fred Masen
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Francis I manipulated the code so much to get the age feature added to my program that all results were wrong. Look above my previous post I added the Gaddis example(original) and I understand it very WELL and it worked perfectly fine. But soon as I tried to add the feature age , it is missing up with the "total" then it is probably missing a for loop age to work. I just don't know how to do it with that age and average age feature.
     
    Fred Masen
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Basically it is like ok I understand that very well let me try to add an average age feature into that program. And then I can't do it !
     
    salvin francis
    Bartender
    Posts: 2899
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I have asked a couple of questions above, I haven't received answers for any of them
     
    Fred Masen
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I understand Francis but as I said I changed the code so much that it is not behaving logically as results. Look at the above example from Gaddis and the results are perfectly fine. The issue for me is to add an age & average age feature to that program without messing up the program.
     
    Fred Masen
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    See :

    Program Output with Example Input Shown in Bold
    How many students do you have? 3 [Enter]
    How many test scores per student? 3 [Enter]
    223224
    Chapter 4   Loops and Files
    Student number 1
    -----------------------
    Enter score 1: 100 [Enter]
    Enter score 2: 95 [Enter]
    Enter score 3: 90 [Enter]
    The average for student number 1 is 95.0.
    Student number 2
    ----------------------
    Enter score 1: 80 [Enter]
    Enter score 2: 81 [Enter]
    Enter score 3: 82 [Enter]
    The average for student number 2 is 81.0.
    Student number 3
    ----------------------
    Enter score 1: 75 [Enter]
    Enter score 2: 85 [Enter]
    Enter score 3: 80 [Enter]
    The average for student number 3 is 80.0.
     
    Campbell Ritchie
    Marshal
    Posts: 74381
    334
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Fred Masen wrote:Here's the Gaddis example from the book  . . .

    Which book? Please always say where material comes from. I presume you copied that example, but in the process you have unfortunately lost the indentation. Indentation is important, and I am not simply being fussy; otherwise it becomes so much more difficult to understand which part of the code is which.
    Did they have all those comments? Comments might be good, but most of these comments should be deleted because they only tell us what is obvious from reading the code. The variable names given are at best vague; I can't tell from the name total what you are adding up

    I think you would have more idea about what is going wrong if you had followed the last suggestion I made.
     
    Fred Masen
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The book is : Starting Out with Java: From Control Structures through Objects (6th Edition) .  It is follow by many computer sciences schools.
    The example I have is from that book :
    - However to improve my programming skills I try to always add a new feature in the program example which is the "age feature(average)
    However in this program it seems that when I implement that age feature it's giving wrong results for the entire program (score age etc..)
    - I try to add a second total1 and second ageAverage  to avoid  messing up with the score feature. Also I don't need an age average in each student
    all I need is an age average after the end of the program which makes since. I tried to figure out that I could get a method outside
    the for student loop but it will not work . Anyway here's the example of the book :

    The program in Code Listing 4-13 shows another example. It is a program that a teacher
    might use to get the average of each student’s test scores. In line 22 the user enters the num-
    ber of students, and in line 26 the user enters the number of test scores per student. The for
    loop that begins in line 29 iterates once for each student. The nested inner for loop, in lines
    36 through 41, iterates once for each test score.


    How many students do you have? 3 [Enter]
    How many test scores per student? 3 [Enter]

    Student number 1
    -----------------------
    Enter score 1: 100 [Enter]
    Enter score 2: 95 [Enter]
    Enter score 3: 90 [Enter]
    The average for student number 1 is 95.0.
    Student number 2
    ----------------------
    Enter score 1: 80 [Enter]
    Enter score 2: 81 [Enter]
    Enter score 3: 82 [Enter]
    The average for student number 2 is 81.0.
    Student number 3
    ----------------------
    Enter score 1: 75 [Enter]
    Enter score 2: 85 [Enter]
    Enter score 3: 80 [Enter]
    The average for student number 3 is 80.0.
     
    Piet Souris
    Bartender
    Posts: 4691
    183
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The code sofar shows a lot of variables that must be maintained while processing the input.

    Why not structure the input a little? Suppose you have a class Student:

    class Student {
       String name;
       int age;
       List <Double> scores = new ArrayList <>();

       Student(String name, int age) {....}

       public void addScore (double score){...}

       public int getAge (...)

       public double getAverageScore (...)

       public double getSumOf scores

       public int get numberOfScores
    }

    With such a class your administration will be much easier. The only things not directly available are the average score over all students and the average age.

    In your input section,  you can have a List <Student>. Any idea how you could derive the average age?
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic