• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Changing code to display lowest value  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm fairly new to using Java and cannot figure out how to change it from displaying my highest value to displaying my lowest value
if you could just show me it would be very appreciated, i"m sure its simple or I think it should be...















import java.util.Scanner;
public class JNAssignment21
{
  public static void main(String[] args)
  {
    Scanner k = new Scanner(System.in);
    int m = 10;
    double [] n = new double [m];
    for(int i=0; i<10; i++)
    {
      System.out.print("Please enter integer " + (i+1)+ ":");
      n[i]=k.nextInt();
     
    }
    System.out.println("");
    System.out.print("You have entered: ");
    for (int i=0; i<10; i++)
    {
      double num =n[i];
      System.out.print (num + " ");
    }
    System.out.println("");
    double max = maximum(n);
    System.out.println("The bggest number of the 10 integers is " +max);
   
  }
 
  public static double maximum ( double [] n)
  {
    double max = n[0];
    for (int i=0; i<n.length; i++)
    {
      if (n[i]>max)
      {
        max=n[i];
      }
    }
    return max;
  }
}
 
Sheriff
Posts: 23710
50
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jaedon, welcome to the Ranch!

You're right that it's simple (or fairly simple, anyway). So let's start: what part of the code do you think is calculating the highest value? You'd need to change that code to calculate the lowest value.
 
Marshal
Posts: 60136
188
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranc again.
Paul is right that you only need a simple change to the code to find the least value in that array.
Please in future use the code button; then your code will look like this:-
 
jaedon nixon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Hi jaedon, welcome to the Ranch!

You're right that it's simple (or fairly simple, anyway). So let's start: what part of the code do you think is calculating the highest value? You'd need to change that code to calculate the lowest value.



Im pretty sure lines 30 & 31 are the lines determining the largest number in the code.
 
Campbell Ritchie
Marshal
Posts: 60136
188
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the code carefully. You are not far out, but 30 and 31 isn't correct. If you are seeking the minimum, you will want to write a new method with max changed to min throughout.
 
jaedon nixon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See but i don't completely understand the code, basically teacher hands us this and we copy it out. im getting lost when im trying to understand the code.
 
jaedon nixon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Go through the code carefully. You are not far out, but 30 and 31 isn't correct. If you are seeking the minimum, you will want to write a new method with max changed to min throughout.

 
Campbell Ritchie
Marshal
Posts: 60136
188
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jaedon nixon wrote:. . . teacher hands us this and we copy it out. im getting lost  . . .

I have never thought that simply reading code is a good way to learn programming. I suggest you go through the method to find a maximum value and read out what the code says. Read it aloud. I hope that will make it easier to understand.
Draw a diagram with the entries in the array (you simply need ten numbers) and go through that loop with pencil and paper. If you are stuck for ten numbers run this code:-That will produce an array of ten numbers “randomly” chosen from -1000.0...999.999... You can of course change the arguments.

[edit]Needed one more )
 
Greenhorn
Posts: 3
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jaedon, I'll try to walk you through the method step by step and maybe that can help you figure out how to find the minimum. I'll simply add comments to the method the teacher gave you.



I don't know what level of Java you are at, but I explained it as though this was your very first day of looking at Java.
 
jaedon nixon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I have never thought that simply reading code is a good way to learn programming... [edit]Needed one more )



do you think you could point out where max is defined and kinda explain how its being explained? i think that might help me
 
jaedon nixon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lombe Chileshe wrote:Hi Jaedon, I'll try to walk you through the



Thank you this was helpful

I think i can figure it out from what you'v shown me will report back if i figure it out or get stuck again with no more ideas
 
Campbell Ritchie
Marshal
Posts: 60136
188
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jaedon nixon wrote:. . . point out where max is defined . . .

Line 30 in the original code. You have a second variable which is completely separate but has the same name in line 23.
I think you have got a good explanation of the maximum() method from LC.
 
Campbell Ritchie
Marshal
Posts: 60136
188
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lombe Chileshe, welcome to the Ranch
 
jaedon nixon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out, and I think i understand the code a bit more now, thank you guys for the help . sorry for all the questions lol probably will be around here more often one of the better forums that I'v found
 
Campbell Ritchie
Marshal
Posts: 60136
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jaedon nixon wrote:. . . thank you . . .

That's a pleasure

sorry for all the questions . . .

That is what the forum is for. I am pleased that you worked it out; please show us your solution.
 
Ranch Hand
Posts: 90
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey are you using an IDE  like net beans or eclipse. When I was first starting to learn Java  i waould use the debugger to walk me through code I was having issues with. This really helped me understand what the code was doing step by step. I'm still learning an I still use the debugger to help me grasp code better.
 
Ranch Hand
Posts: 84
3
MS IE Notepad Suse
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jaedon nixon wrote:See but i don't completely understand the code, basically teacher hands us this and we copy it out. im getting lost when im trying to understand the code.



Sorry for going off-topics here, but I highly doubt that statement. Tearchers' there to teach you something, but each year you raise teachers can expect more done by yourself after explaining what's given to you.

As you learn programming in Java, we can assume you're told what the code does you're given and how it works. From someone in your age your teach could expect that ypu have basic knowledge in maths and algebra to under stand how a maximum is calculated and what to change to get minimum. The programming language Java is just another way to describe the algebra what someone (in this case the computer executing your code) has to do.

This may sound rough, but if you don't have the needed basics to learn programming you should stop trying it and refresh you basic skills in maths, otherwise you will end up us doin ypur homework. Each programming forum has seen lot of such failures and coutinue to. That's what separating those willing to learm and be teady to do some for it - and those who just sit there with kindof "I don't like to be here" attitude.
 
Sheriff
Posts: 5121
138
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jaedon nixon wrote:I figured it out, and I think i understand the code a bit more now...


Matt Wong: I think the OP should be praised for learning something new, rather than scolded.  Regardless, this forum is for helping beginners learn Java.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!