how find the largest and smallest number top-down approach using java

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 3133 Views - Last Post: 06 July 2012 - 04:00 AM Rate Topic: -----

#1 braincofe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-July 12

how find the largest and smallest number top-down approach using java

Posted 03 July 2012 - 01:45 AM

Hello fellow..I am quite new here..
i know small about java..but this is beyond my limitation.I create a code that infinite input and find the largest and
smallest number by using top-down approach..to stop the loop i use number 0.
Still i don't get the right out put cause what is the last input its also the output..
I would appreciate some assistance in the form of making me aware of some sort of hints as to what I could do to accomplish this task.

Here is the code that I have so far:
class largestandsmallestTopdown{
	public static void main(String[]args){
		int input;
		int max;
		int min;
		print("Enter Integer: ");
		input=IntegerInput();
		max=LargeInt(input);
		min=SmallestInt(input);
		println("The largest: "+max
		      +"\nThe Smallest: "+min);
		}

		static void print(String str){
		    System.out.print(str);
			}
		static int IntegerInput(){
			Scanner sc = new Scanner(System.in);
			int input;
			input = sc.nextInt();
				while(input>0){
					System.out.print("Enter Integer: ");
					input = sc.nextInt();

					}
				return input;
			}
		static int LargeInt(int input){//The problem is here..the out put should be 
			int max=0;              //the largest number
			if(input>max){
				max=input;
				}
			return max;
			}
		static int SmallestInt(int input){//And here the out put should be
			int min=9999;            //the smallest number
			if(input<min){
				min=input;
				}
			return min;
			}
		static void println(String str){
			System.out.println(str);
			}
	}



Thanks to all of you in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: how find the largest and smallest number top-down approach using java

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1688
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: how find the largest and smallest number top-down approach using java

Posted 03 July 2012 - 03:18 AM

In your code the user enters only a single integer. And then you want to find the minimum and the maximum of that one integer? That seems a bit strange to me.
Was This Post Helpful? 1
  • +
  • -

#3 braincofe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-July 12

Re: how find the largest and smallest number top-down approach using java

Posted 03 July 2012 - 07:11 AM


17	        static int IntegerInput(){                         //In this area the user can infinite input
18	            Scanner sc = new Scanner(System.in);           //sorry about for lack information
19	            int input;                                     //until when the 0 press the loop will stop
20	            input = sc.nextInt();                          //but zero should be count too right?.
21	                while(input>0){                            //I think i should use -1 to stop the loop
22	                    System.out.print("Enter Integer: ");
23	                    input = sc.nextInt();
24	 
25	                    }
26	                return input;
27	            }


Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1688
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: how find the largest and smallest number top-down approach using java

Posted 03 July 2012 - 07:18 AM

Okay, so the user is entering more than one integer. But you're only returning the last one. Your input variable has type int. That means it contains a single integer. You return that single integer and then you're passing that single integer to your LargeInt and SmallestInt methods. Simply put: any function called SmallestInt that takes one argument of type int doesn't make any sense.

You should store the user input in some kind of collection - for example an ArrayList.
Was This Post Helpful? 0
  • +
  • -

#5 braincofe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-July 12

Re: how find the largest and smallest number top-down approach using java

Posted 03 July 2012 - 07:54 AM

Ahh...I see..But if i use ArrayList there should be a limiter and the user must first input the limiter of the array and then the integer.
Is there a possible in the ArrayList that the user can input as many as he want without inputting the limiter..?

By the way sir sepp2k thanks for the information about "int" now i know that its only contain one integer.. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1688
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: how find the largest and smallest number top-down approach using java

Posted 03 July 2012 - 07:59 AM

I don't know what you mean by limiter, but you don't need the user to enter anything special to use an ArrayList.
Was This Post Helpful? 0
  • +
  • -

#7 braincofe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-July 12

Re: how find the largest and smallest number top-down approach using java

Posted 03 July 2012 - 08:24 AM

i found some example about arraylist..it has a limiter can you change this into infinite input...
because when i change the code its getting more complicated..
because this is my first time to encounter using arraylist..
public class samplecode {
  public static void main (String [] args) {
      ArrayList<Integer> pos = new ArrayList<Integer>();
      ArrayList<Integer> neg = new ArrayList<Integer>();
      Scanner sc = new Scanner(System.in);
		
	System.out.print("enter an integer: ");
	
      int i=0 ;

      while(i <= 10) { //Limiter or cut-off
          System.out.print("enter an integer: ");
          int next_int = sc.nextInt();
          if(next_int < 0) { 
              neg.add(next_int);
          } else {
              pos.add(next_int);
          }

          i++;
      }
      System.out.println("positive numbers" + pos);
      System.out.println("negative numbers" + neg);
   }
}



thanks advanced.
Was This Post Helpful? 0
  • +
  • -

#8 braincofe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-July 12

Re: how find the largest and smallest number top-down approach using java

Posted 03 July 2012 - 08:42 AM

sir sepp2k i just got the code that the user can now input as many as he/she want...thank you so much for to the help..more power
if you have some additional about this code..i would appreciate that...

public class samplecode {
  public static void main (String [] args) {
      ArrayList<Integer> pos = new ArrayList<Integer>();
      ArrayList<Integer> neg = new ArrayList<Integer>();
      Scanner sc = new Scanner(System.in);
      System.out.print("enter an integer: ");

		int next_int=sc.nextInt();

     while(next_int >0 || next_int < 0) {
          System.out.print("enter an integer: ");
           next_int = sc.nextInt();
          if(next_int < 0) {
              neg.add(next_int);
          } else {
              pos.add(next_int);
          }


      }
      System.out.println("positive numbers" + pos);
      System.out.println("negative numbers" + neg);
   }
}


Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1688
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: how find the largest and smallest number top-down approach using java

Posted 03 July 2012 - 08:46 AM

View Postbraincofe, on 03 July 2012 - 05:24 PM, said:

i found some example about arraylist..it has a limiter can you change this into infinite input...


That code uses i <= 10, so that it knows when to stop. Your code checks whether input is greater than 0, so that it knows when to stop. Both of those are perfectly valid approaches depending on the context and there's no reason why using an ArrayList should affect which one you pick.

Just take the code you already have, create an ArrayList before the loop and add the line myArrayList.add(input) inside the loop.

This post has been edited by sepp2k: 03 July 2012 - 08:47 AM

Was This Post Helpful? 0
  • +
  • -

#10 braincofe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-July 12

Re: how find the largest and smallest number top-down approach using java

Posted 03 July 2012 - 08:48 AM

i will post here soon my main task the largest and smallest number top-down approach...
if theirs some problem....
By the way thanks you so much for the help..
Was This Post Helpful? 0
  • +
  • -

#11 braincofe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-July 12

Re: how find the largest and smallest number top-down approach using java

Posted 03 July 2012 - 10:56 AM

import java.util.Scanner;
import java.util.*;
import java.util.ArrayList;

class largestandsmallestTopdown{
	public static void main(String[]args){

		int input;
		int max;
		int min;
		print("enter the integer: ");
		input=IntegerInput();
		max=LargestInt(input);   //is this valid if i using arraylist
		min=SmallestInt(input);  //is this valid if i using arraylist
		println("Largest: "+max);   
			+"\nSmallest: "+min}

		static void print(String str){
			System.out.print(str);
			}
		static int IntegerInput(){
			ArrayList<Integer> num = new ArrayList<Integer>();
			Scanner sc = new Scanner(System.in);
			int input= sc.nextInt();
			while(input>0){
				System.out.print("enter the integer: ");
				input=sc.nextInt();
						num.add(input);

				}
				return num.????();//what should i write here
			}
		static ???? LargeInt(int input ){ //what should i write here
			what should i write here
			}
		static ???? SmallestInt(int input){ //what should i write here
			what should i write here
			}
		static void println(String str){
			System.out.println(str);
			}

	}



thanks advanced

sorry for my newbiesness....
Was This Post Helpful? 0
  • +
  • -

#12 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1688
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: how find the largest and smallest number top-down approach using java

Posted 03 July 2012 - 11:21 AM

Okay, first of all you need to move num.add(input) one line up. Otherwise it will include the 0 the user inputs at the end, but not the first number that the user inputs.

Then line 31 should just be return num;. You don't want to return a single number from the list or any property of the list - you want to return the whole list. So return num; is what you want. To make this work however, you need to change the return type to ArrayList<Integer> (or List<Integer>) because currently the method signature still claims that you're returning a single integer.

Regarding the methods LargeInt and SmallestInt: The return type should still say int, because those methods do return a single integer. They should however take a list of integers as arguments. I.e. you should change the type of the parameter input (to List<Integer>), not the return type.
Was This Post Helpful? 0
  • +
  • -

#13 braincofe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-July 12

Re: how find the largest and smallest number top-down approach using java

Posted 03 July 2012 - 11:20 PM

import java.util.Scanner;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;

class largestandsmallestTopdown{
	public static void main(String[]args){

		ArrayList<Integer> input;
		int max;
		int min;
		print("enter the integer: ");
		input=IntegerInput();
		min=SmallestInt(input);
		max=LargestInt(input);
		println("Largest: "+max
			  +"\nSmallest: "+min);
			}

		static void print(String str){
			System.out.print(str);
			}
		static ArrayList<Integer> IntegerInput(){
			ArrayList<Integer> num = new ArrayList<Integer>();
			Scanner sc = new Scanner(System.in);
			int input= sc.nextInt();
			num.add(input);
			while(input>0){
				System.out.print("enter the integer: ");
				input=sc.nextInt();
				}
				return num;
			}
		static int LargestInt(ArrayList<Integer> input ){ 
			Object max = Collections.max(input);      
			return max;                               //still some errors here
			}
		static int SmallestInt(ArrayList<Integer> input ){
			Object min = Collections.min(input);    
			return min;                              //still some errors here
			}
		static void println(String str){
			System.out.println(str);
			}

	}



if you some correction about this code i would appreciate..

thanks advanced again..
Was This Post Helpful? 0
  • +
  • -

#14 busyme12srv  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 44
  • Joined: 18-June 12

Re: how find the largest and smallest number top-down approach using java

Posted 04 July 2012 - 12:36 AM

You can try this code for finding min/max of a list of numbers:




public class findmaxmin{

int input, a[10],min,max;

//Store elements 
public  int IntegerInput(){  

            Scanner sc = new Scanner(System.in);  
              
            for (int i=0;i<10;i++)
             {
             input = sc.nextInt(); 
              a[i] = input;
              }
               

}

//Find minimum 
public int findmin()
{
min = a[0];
for (i=0;i< 10;i++)
 {
       if (min > a[i])
        min = input;
}

//Find Maximum
public int findmax()
{
max=a[0];
for (i=0;i< 10;i++)
 {
   
     if (max < input)
        max = input;
}

}
public static void main(String[]args){ 
findmaxmin m = new findmaxmin();
m.IntegerInput();
m.findmin();
m.findmax();
}
}
         




Was This Post Helpful? -1
  • +
  • -

#15 braincofe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-July 12

Re: how find the largest and smallest number top-down approach using java

Posted 04 July 2012 - 07:00 PM

sir busyme12srv how can i make this one to infinite input..?
can u make this one to infinite input...I would appreciate..
Thank you advanced.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2