0

I'm in the process of learning java. Today, my task was to find the errors in some code. I'm in the process of working on it, but I have no idea why the following error occurs. In the sample code, it gives me a ".class expected" on the line "int momsAge= 42; dadsAge= 43;" specifically, it puts an error line right infront of momsAge.

// A class that computes the sample statistics of the ages of
// family members.
public class FamilyStats
{
public static void main(String[] args)
{
/*
math equations obtained from: */
http://mathworld.wolfram.com/SampleVariance.html

// define some ages

int momsAge= 42; dadsAge= 43;
int myAge= 22, sistersAge= 16;
int dogsAge= 6;

// get the mean

double ageSum = (momsAge + dadsAge + myAge + sistersAge + DogsAge);
double average = ageSum / 5

/ calculate the sample variance
double variance= 0.0;
variance += (momsAge - average)*(momsAge - average);
variance += (dadsAge - average)(dadsAge - average);
variance += (myAge - average)*(myAge - average);
variance += (sistersAge - average)*(sistersAge - average);
variance += (dogsAge - average)*(dogsAge - average);
variance = variance / 4;

// get the std. dev
double standardDev= Math.sqrt(variance);
// output the results
System.out.println("The sample age mean is: " + average);
System.out.println("The sample age variance is: " + variance);
System.out.println("The sample age standard deviation is: " + standardDev);
}
}

2 Answers 2

4
int momsAge= 42; dadsAge= 43;

should be

int momsAge= 42, dadsAge= 43;

The ; serves as the end of the statement, thus essentially making your next statement

dadsAge = 43;

Obviously this is wrong because a class is expected. Using the , allows you to chain those assignments.

Also:

http://mathworld.wolfram.com/SampleVariance.html

isn't commented (at least the http: part isn't).

Sign up to request clarification or add additional context in comments.

2 Comments

or insert another int after the semicolon -> int momsAge= 42; int dadsAge= 43;
So, I went back and changed that (thanks), but it is still giving me a .class expected error, in the same place.
0

Here's the correct code with the inline corrections that I made:

// A class that computes the sample statistics of the ages of
// family members.
public class FamilyStats {

    public static void main(String[] args) {
        /*
         math equations obtained from: 
         http://mathworld.wolfram.com/SampleVariance.html
         */ 
       //^--Included the url into the comments
// define some ages
        int momsAge = 42, dadsAge = 43;
                      //^--Changed ; to ,
        int myAge = 22, sistersAge = 16;
        int dogsAge = 6;

// get the mean

        double ageSum = (momsAge + dadsAge + myAge + sistersAge + dogsAge);
        double average = ageSum / 5;
        // calculate the sample variance
       //^--Added the second / to mark the line as a comment
        double variance = 0.0;
        variance += (momsAge - average) * (momsAge - average);
        variance += (dadsAge - average) * (dadsAge - average);
        variance += (myAge - average) * (myAge - average);
                                    //^--Added missing * symbol
        variance += (sistersAge - average) * (sistersAge - average);
        variance += (dogsAge - average) * (dogsAge - average);
        variance = variance / 4;

// get the std. dev
        double standardDev = Math.sqrt(variance);
// output the results
        System.out.println("The sample age mean is: " + average);
        System.out.println("The sample age variance is: " + variance);
        System.out.println("The sample age standard deviation is: " + standardDev);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.