Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Deadline coming up in a few hours, can't figure out what I'm doing wrong  RSS feed

 
Greenhorn
Posts: 9
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exercise:


My files:
ArrayStatistics class


TestStatistics class


Main class
[img]https://i.imgur.com/Gvw9QpP.jpg
[/img]

Errors:
 
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
none of your methods return anything.

Please don't post screen snapshots, they are very hard to read. Cut and paste the text into the post.
 
Suane Mane
Greenhorn
Posts: 9
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are they suposed to return?

Here is the code instead, sorry for the images.

ArrayStatistics class



TestStatistics class


Main class
 
Carey Brown
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You already declare the type each method should be returning, you're just not doing it. I would presume that you are getting compile errors on this.

Example:
public static double sum(double[] x) {
 
Suane Mane
Greenhorn
Posts: 9
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't the return already stated in the other class (TestStatistics)? That's the one that actually runs the methods and prints the results.
 
Carey Brown
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This says that it is expecting sum(x) to return something, though it doesn't say what.
This says that sum() is going to return a double, but then doesn't do it.
Here you create a local variable named "sum" which you use to calculate the sum but you never do anything with it, so when the method ends, the local variable sum, disappears along with all the work done to compute it. Nothing is returned.
 
Suane Mane
Greenhorn
Posts: 9
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how I'm supposed to write a return statement then. I thought the parts like:

declare what needs to be done when the method is accessed and I thought x was the array? And thus it would go through all array items and just add them up.
 
Carey Brown
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Suane Mane wrote:I don't know how I'm supposed to write a return statement then. I thought the parts like:

declare what needs to be done when the method is accessed and I thought x was the array? And thus it would go through all array items and just add them up.

It certainly does sum them up but the sum is not being returned.
 
Suane Mane
Greenhorn
Posts: 9
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've added that now and still getting errors.
 
Carey Brown
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a) Did you fix it in all your methods that declare that they are returning a value?
b) When you say you're getting errors, you need to cut and paste the complete error message into a post.
c) Re-post your code after you've made changes so we can keep in sync and that the error messages are in sync with your line numbers.
 
Suane Mane
Greenhorn
Posts: 9
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I think I have it now. The problem was having 2 main methods. I didn't know how to approach this problem so I used the same solution as in previous labs where we were told to make a separate Main class with a main method to call other methods. Apparently it's useless here, as there is already a main method declared in the class TestStatistics. So this is the code now:





Results
Array = [1.0, -2.0, 3.0, -4.0, 5.0]
Sum = 3.0
Average = 0.6
Min = -4.0
Max = 5.0

It actually worked! Thanks for the help!
 
Carey Brown
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is giving you some of the errors. ArrayStatistics.sum() requires a double[] as an argument. Ditto for the other calls. You are throwing away the returned values from all of these methods, this won't give you a compile error but is somewhat pointless.

It seems that your TestStatistics class is what this class should be. In other words:
TestStatistics class -- good
Main class -- compiles but pointless
 
Carey Brown
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A note on your style

It is recommended that conditional and loop statements always be enclosed in braces. This is especially true for beginners. So you should have
 
Greenhorn
Posts: 7
1
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I would take a step back and think about what the exercise is trying to achieve.

Here are some questions they are asking...

  • We are given a static class that has a main, we know that this class will start the execution of our program. Do I know how to compile and run a java class?
  • We are told to create a class. Do I know how to create a class?
  • We are told to add 4 static methods to that class. We are given the complete function definition, nice! Do I know what methods of a class are?
  • Do I know what public and static mean? Do I know what the double following public static means? Do I know what sum(double[] x) means?
  • We keep seeing static. What does that mean when my class is static. How does that change how methods are called?
  • Do I know how to iterate through an array?
  • Do I know how to return a value from a method?

  •  
    Suane Mane
    Greenhorn
    Posts: 9
    IntelliJ IDE Java MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oooh I see. I forgot about that. Too many braces everywhere! At least it works even without, but I see it's cleaner to work with like this and easier to follow the logic when looking back at it. Thanks!
     
    Suane Mane
    Greenhorn
    Posts: 9
    IntelliJ IDE Java MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Kyle Prouty wrote:
    I would take a step back and think about what the exercise is trying to achieve.

    Here are some questions they are asking...

  • We are given a static class that has a main, we know that this class will start the execution of our program. Do I know how to compile and run a java class?
  • We are told to create a class. Do I know how to create a class?
  • We are told to add 4 static methods to that class. We are given the complete function definition, nice! Do I know what methods of a class are?
  • Do I know what public and static mean? Do I know what the double following public static means? Do I know what sum(double[] x) means?
  • We keep seeing static. What does that mean when my class is static. How does that change how methods are called?
  • Do I know how to iterate through an array?
  • Do I know how to return a value from a method?



  • Yeah, this is what I am trying to do with each exercise. I've struggled with Java last year and now am getting back on track and trying to do everything properly, learn from scratch. I was trying to figure out what the question was asking, what each part of code they provided actually means and what is expected of me to know so far and add in. I may be slow or something because this was quite hard for me. Been at it for hours.
    I've found that Java theory and actual definitions are MUCH harder to understand than actually working with code. I was so motivated to write down everything, grabbed my notebook, sat down and looked at first few slides talking about classes, packages, methods and source files. I got stuck immediately trying to make sense of the hierarchy and what goes where and is a class a branch of a package? Is a method a child of a class? Is a class same thing as a source file then? I was frankly more confused after reading the slides than after trying to write a simple program in lab. I don't know if I'll ever understand the actual theory of Java.
     
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can also write the source codes like below:






     
    Mark Ii
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you look at it I used POSITIVE_INFINITY and NEGATIVE_INFINITY fields of Double class to show you, a program can be written in many ways. I hope it helps.
     
    Kyle Prouty
    Greenhorn
    Posts: 7
    1
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Here is a fun Java 8 way to do it.

     
    Ranch Hand
    Posts: 211
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is why I stopped doing college courses on programming.  If you are asked to do an assignment of which you never learned how to write methods first, it's a bad course.  

    Every method except for void has to have a return statement of the type of value you are writing.
     
    Marshal
    Posts: 62881
    203
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Kyle Prouty wrote:Here is a fun Java 8 way to do it. . . .

    Careful; that code is broken and will fall over horribly if you pass a 0‑element array to it.
    Try
     
    Suane Mane
    Greenhorn
    Posts: 9
    IntelliJ IDE Java MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Kyle Prouty wrote:
    Here is a fun Java 8 way to do it.



    I have no idea what final, stream or getasdouble mean. I don't even fully understand the format of it. Is it stuff you learn later on?
     
    Carey Brown
    Saloon Keeper
    Posts: 5485
    55
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes. Later on. Anytime someone mentions "Java 8" it is usually not a beginner topic, and typically involves streams and or functional programming. These features were introduced in Java 8.
     
    Campbell Ritchie
    Marshal
    Posts: 62881
    203
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Suane Mane wrote:. . . I have no idea what final, stream or getasdouble mean. . . .

    Please look here; I only worked out the average.

    A couple of weeks ago, we had a long discussion about whether you should or shouldn't teach Streams to beginners; I think you should. Please look here. It makes very interesting reading.
     
    All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
    RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
    https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!