14 Replies - 638 Views - Last Post: 19 July 2012 - 11:24 AM Rate Topic: ***-- 1 Votes

#1 JoePatriot  Icon User is offline

  • D.I.C Head

Reputation: -20
  • View blog
  • Posts: 78
  • Joined: 12-July 12

Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 09:55 AM

Anyone have any ideas on how to repair this?

Here is the code:
import java.io.IOException;
import java.math.*;
import javax.swing.JOptionPane;

public class Student 
{
    public static void main(String[] args) 
    {
       //double(quiz1, quiz2, quiz3, quiz4, average, TotalScore);
        String name = JOptionPane.showInputDialog(null, "What is your name?");
       JOptionPane.showMessageDialog(null, "Hello, " + name + ".");
        String quiz1 = JOptionPane.showInputDialog(null, "Enter Quiz 1 score");
       JOptionPane.showMessageDialog(null,name + ", your Quiz 1 score is " + quiz1 + " points.");
        String quiz2 = JOptionPane.showInputDialog(null, "Enter Quiz 2 score");
       JOptionPane.showMessageDialog(null,name + ", your Quiz 2 score is " + quiz2 + " points.");
        String quiz3 = JOptionPane.showInputDialog(null, "Enter Quiz 3 score");
       JOptionPane.showMessageDialog(null,name + ", your Quiz 3 score is " + quiz3 + " points.");
        String quiz4 = JOptionPane.showInputDialog(null, "Enter Quiz 4 score");
       JOptionPane.showMessageDialog(null,name + ", your Quiz 4 score is " + quiz4 + " points.");
       
        String TotalScore = quiz1 + quiz2 + quiz3 + quiz4;
       JOptionPane.showMessageDialog(null, name + ", your totalscore is " + TotalScore);
       
        String average = quiz1 + quiz2 + quiz3 + quiz4 / 4;
       JOptionPane.showMessageDialog(null, name + ", your current Average is: " + average + "%");
    }
}



and the output:

run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - bad operand types for binary operator '/'
first type: java.lang.String
second type: int
at Student.main(Student.java:28)
Java Result: 1
BUILD SUCCESSFUL (total time: 14 seconds)

made using Netbeans.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with calculating Student Quizscores/ Averages

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9158
  • View blog
  • Posts: 33,979
  • Joined: 27-December 08

Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 09:59 AM

You can't perform math on Strings. Also, you could simplify this a lot with loops.
int total = 0;
for(int i = 1; i <= 4; i++){
   String input = JOptionPane.showInputDialog("Enter quiz " + i);

   //notice how I use Integer.parseInt() to convert a String to an int
   total += Integer.parseInt(input); 
}

//now divide total/4 to get the average


Was This Post Helpful? 2
  • +
  • -

#3 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1008
  • View blog
  • Posts: 2,250
  • Joined: 05-April 11

Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:05 AM

On top of what has already been said, your calculation is wrong
quiz1 + quiz2 + quiz3 + quiz4 / 4;



Should be
(quiz1 + quiz2 + quiz3 + quiz4) / 4;


Was This Post Helpful? 1
  • +
  • -

#4 JoePatriot  Icon User is offline

  • D.I.C Head

Reputation: -20
  • View blog
  • Posts: 78
  • Joined: 12-July 12

Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:09 AM

How would you recommend That I include this in my current code?
Was This Post Helpful? 0
  • +
  • -

#5 x68zeppelin80x  Icon User is offline

  • D.I.C Regular

Reputation: 50
  • View blog
  • Posts: 279
  • Joined: 07-March 09

Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:18 AM

View PostJoePatriot, on 19 July 2012 - 01:09 PM, said:

How would you recommend That I include this in my current code?


int total = 0;
int i = 1;

while(i <= 4){
   String input = JOptionPane.showInputDialog("Enter quiz " + i++);

   //notice how I use Integer.parseInt() to convert a String to an int
   total += Integer.parseInt(input); 
}

//now divide total/4 to get the average
int avg = total / i;


Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9158
  • View blog
  • Posts: 33,979
  • Joined: 27-December 08

Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:21 AM

Let's not do all his work for him, x68zeppelin80x.

Quote

How would you recommend That I include this in my current code?

Look at the logic. What does each step do? Make an attempt at reworking the logic based on what you are trying to accomplish.
Was This Post Helpful? 0
  • +
  • -

#7 JoePatriot  Icon User is offline

  • D.I.C Head

Reputation: -20
  • View blog
  • Posts: 78
  • Joined: 12-July 12

Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:25 AM

I like your idea of using loops, though i don't believe i should do that yet.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9158
  • View blog
  • Posts: 33,979
  • Joined: 27-December 08

Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:25 AM

Don't be afraid to use it, just because your teacher hasn't covered it yet. Most things you will learn about programming you will end up teaching to yourself.
Was This Post Helpful? 1
  • +
  • -

#9 x68zeppelin80x  Icon User is offline

  • D.I.C Regular

Reputation: 50
  • View blog
  • Posts: 279
  • Joined: 07-March 09

Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:28 AM

View Postmacosxnerd101, on 19 July 2012 - 01:21 PM, said:

Let's not do all his work for him,


All I did was take the code you provided him, and make 'i' global. If anyone helped him it was you.
Was This Post Helpful? 0
  • +
  • -

#10 JoePatriot  Icon User is offline

  • D.I.C Head

Reputation: -20
  • View blog
  • Posts: 78
  • Joined: 12-July 12

Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:28 AM

I know, I just don't want to become too involved before I'm ready to.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9158
  • View blog
  • Posts: 33,979
  • Joined: 27-December 08

Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:31 AM

Quote

All I did was take the code you provided him, and make 'i' global.

You basically finished the assignment. Also, 'i' is not a global. A global is a static variable.

Quote

I know, I just don't want to become too involved before I'm ready to.

You're ready to take the leap. Loops aren't hard. They make your life easier and your code cleaner. Don't hold yourself back. :)
Was This Post Helpful? 0
  • +
  • -

#12 JoePatriot  Icon User is offline

  • D.I.C Head

Reputation: -20
  • View blog
  • Posts: 78
  • Joined: 12-July 12

Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:56 AM

Hey, thanks guys, I finally got it going.

here is the new code.
import java.io.IOException;
import java.math.*;
import javax.swing.JOptionPane;

public class Student 
{
    public static void main(String[] args) 
    {
       //double(quiz1, quiz2, quiz3, quiz4, average, TotalScore);
        String name = JOptionPane.showInputDialog(null, "What is your name?");
       JOptionPane.showMessageDialog(null, "Hello, " + name + ".");
       
       int TotalScore = 0;
       for(int i = 1; i <= 4; i++)
       {
            String input = JOptionPane.showInputDialog("Enter quiz " + i);
               TotalScore += Integer.parseInt(input); 
        } 
       
       JOptionPane.showMessageDialog(null, name + ", your totalscore is " + TotalScore);
       
        int average = TotalScore / 4;
       JOptionPane.showMessageDialog(null, name + ", your current Average is: " + average + "%");
    }
}


Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9158
  • View blog
  • Posts: 33,979
  • Joined: 27-December 08

Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 10:59 AM

Glad we could help! :)
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

Reputation: 5603
  • View blog
  • Posts: 9,044
  • Joined: 19-March 11

Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 11:00 AM

View Postmacosxnerd101, on 19 July 2012 - 12:31 PM, said:

Also, 'i' is not a global. A global is a static variable.


Interesting statement. I think I can see how that would make sense, but could you unpack it a little?
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9158
  • View blog
  • Posts: 33,979
  • Joined: 27-December 08

Re: Problem with calculating Student Quizscores/ Averages

Posted 19 July 2012 - 11:24 AM

Variable scope is defined in three ways: globals, instance fields, and local variables. Globals are static variables in a class. Static variables are associated with the class rather than the individual objects, and are invoked using ClassName.globalName. Globals are often times constants.

Instance variables are declared in the class as well, but not in any methods, and without the static keyword. Instance variables are associated with the individual Objects. For example, if you have a Person class, name, age, and gender would be instance variables. Since each Person can have a unique name, age, and gender.

Local variables are declared inside methods (or blocks of code in the class). Their scope is limited to said method or block of code. For example, given the code below, a is local to foo(). However, x is local to the if statement within foo(). A local variable cannot be accessed beyond its context. So a cannot be accessed outside of foo(), and x cannot be accessed outside of the if statement.
public void foo(){
   int a = 3;

   if(a == 5){
       int x = 3;
   }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1