Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Learning to use the Arrays class, object oriented style

    I'm trying to figure out how to fill an array with a method (in a separate class from the class with the main method), so I can pass it around as an object, but I'm getting some errors I'm not sure how to fix. The errors are in the code comments:
    Arrays.java:
    Java Code:
    public class Arrays {
    	public static void main(String[] args){
    		
    		Arrays numbersAndStuff = new Arrays();
    
    		OneDimensionalArray.fillArray(numbersAndStuff);//error: The method fillArray(int[]) in the 
    		//type OneDimensionalArray is not applicable for the arguments (Arrays)		
    		
    		OneDimensionalArray.numSort(numbersAndStuff);//error:The method numSort(int[]) in the type 
    		//OneDimensionalArray is not applicable for the arguments (Arrays)
    	}
    }
    OneDimensionalArray.java:
    Java Code:
    public class OneDimensionalArray {
    	
    	//instance variables:
    		private Arrays numbersAndStuff;
    		
    		//default constructor:
    		public OneDimensionalArray()
    		{
    			numbersAndStuff = new Arrays;//error: The constructor Arrays() is not visible
    		}
    		//overload constructor:
    		public OneDimensionalArray(Arrays numbersAndStuff)
    		{
    			this.numbersAndStuff = numbersAndStuff;
    		}
    	
    	public static int[] fillArray(int[] numbersAndStuff){
    		int numbersAndStuff[] = new int[10];//error: Duplicate local variable numbersAndStuff
    		Scanner nums = new Scanner(System.in);
    		int currentNum = 0;
    		for(int i = 0; i < numbersAndStuff.length; i++){
    			currentNum++;
    			System.out.println("Enter integer for number " + currentNum + " 10: ");
    			numbersAndStuff[i] = nums.nextInt();
    		}
    		nums.close();
    		return numbersAndStuff;
    	}
    	
    	public static int[] numSort(int[] numbersAndStuff){
    		Arrays.sort(numbersAndStuff);
    		return numbersAndStuff;
    	}
    	
    }//end of OneDimensionalArray class body
    Thanks in advance.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Learning to use the Arrays class, object oriented style

    Have you actually read what the compiler had to say? A method expects an int[] ('int array') but you are supplying it an 'Arrays' object. Also a constructor needs parentheses '()' when you want to call it; and you can't define a local variable with the same name as a parameter.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Re: Learning to use the Arrays class, object oriented style

    Quote Originally Posted by JosAH View Post
    A method expects an int[] ('int array') but you are supplying it an 'Arrays' object.
    In Arrays.java or OneDimensionalArray.java? Which lines?

    Quote Originally Posted by JosAH View Post
    Also a constructor needs parentheses '()' when you want to call it
    If you're talking about line #9 in OneDimensionalArray.java, the error still doesn't go away when I do this:
    Java Code:
    //default constructor:
    		public OneDimensionalArray()
    		{
    			numbersAndStuff = new Arrays();//The constructor Arrays() is not visible
    		}

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Learning to use the Arrays class, object oriented style

    Do you have a class called Arrays?
    If so then that's a mistake as it is also the name of a class in the java.lang package that has a private constructor (it's all static methods). Either way that is the one it is picking up.

    As for the int[]:
    Java Code:
    public static int[] fillArray(int[] numbersAndStuff){
            int numbersAndStuff[] = new int[10];//error: Duplicate local variable numbersAndStuff
    You have a parameter called 'numbersAndStuff' and then you straight away declare a local variable of the same name.
    You can't do that, in the same way that you can't have two local variables with the same name in the same scope.
    Since you appear to be creating a new array and returning it then why have it as a parameter?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Learning to use the Arrays class, object oriented style

    Quote Originally Posted by Sam_JavaTheHut5580 View Post
    In Arrays.java or OneDimensionalArray.java? Which lines?
    Lines #6 and #9, where you quoted the compiler error messages.

    Quote Originally Posted by Sam_JavaTheHut5580 View Post
    If you're talking about line #9 in OneDimensionalArray.java, the error still doesn't go away when I do this:
    Java Code:
    //default constructor:
    		public OneDimensionalArray()
    		{
    			numbersAndStuff = new Arrays();//The constructor Arrays() is not visible
    		}
    Have you checked the API documentation for the Arrays class? I guess not, because the Arrays class doesn't have a public constructor and the compiler tells your about it, but you didn't read it ... (it is a utility class).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Re: Learning to use the Arrays class, object oriented style

    Listening to everything Tolls said in post #4, and what JosAH said in post #2, I fixed what I could:

    Array.java:
    Java Code:
    package Arrays01;
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class Array {
    	public static void main(String[] args){
    		
    		OneDimensionalArray numbersAndStuff = new OneDimensionalArray();//error: The constructor 
    		//OneDimensionalArray() is undefined
    		
    		OneDimensionalArray.fillArray(numbersAndStuff);//error: The method fillArray(int[]) in the 
    		//type OneDimensionalArray is not applicable for the arguments (Arrays)		
    		
    		OneDimensionalArray.numSort(numbersAndStuff);//The method numSort(int[]) in the type 
    		//OneDimensionalArray is not applicable for the arguments (Arrays)
    	}
    }
    OneDimensionalArray.java:
    Java Code:
    package Arrays01;
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class OneDimensionalArray {
    	
    	//instance variables:
    		private Arrays numbersAndStuff;
    		
    		//overload constructor:
    		public OneDimensionalArray(Arrays numbersAndStuff)
    		{
    			this.numbersAndStuff = numbersAndStuff;
    		}
    	
    	public static int[] fillArray(int[] numbersAndStuff){
    		Scanner nums = new Scanner(System.in);
    		int currentNum = 0;
    		for(int i = 0; i < numbersAndStuff.length; i++){
    			currentNum++;
    			System.out.println("Enter integer for number " + currentNum + " 10: ");
    			numbersAndStuff[i] = nums.nextInt();
    		}
    		nums.close();
    		return numbersAndStuff;
    	}
    	
    	public static int[] numSort(int[] numbersAndStuff){
    		Arrays.sort(numbersAndStuff);
    		return numbersAndStuff;
    	}	
    }//end of OneDimensionalArray class body
    However, I'm sorry to say that I'm still struggling (with no luck) to figure out how to apply what JosAH said in post #2:
    Quote Originally Posted by JosAH View Post
    A method expects an int[] ('int array') but you are supplying it an 'Arrays' object.
    Instantiating an array object is definitely a different animal than all of the other types of object instantiations that I've successfully gotten to work so far. I already suspect that the error in Array.java on line #8 has to do with the fact that the Arrays utility class doesn't have a public constructor, but I still don't know where the syntax error is coming from, because I've already got rid of my public default constructor. Is the syntax on lines 16 and 28 in OneDimensionalArray.java still wrong?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Learning to use the Arrays class, object oriented style

    What exactly is it you are trying to achieve?

    You seem to have a random scattering of classes and methods which look more like you have attempted to fix issues by taking the suggestions from an IDE.
    IDEs aren't terribly smart.

    To take an example. Why are you importing java.util.Arrays (I had the packages wrong earlier)?
    You don't use it except by mistake on line 11 of OneDimensionalArray.

    What is 'numbersAndStuff' supposed to be?
    You currently have variables and parameters of that name with three different classes, a OneDimensionalArray, an Arrays and an int[].
    You need to figure out what exactly that is supposed to be and then be consistent.

    Finally (for now) you cannot pass something into a method as a parameter that doesn't match the types of the parameters for that method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Learning to use the Arrays class, object oriented style

    Regarding the Arrays class, it is the first class shown in his code. Then later the OP changed it to Array but still tried to instantiate Arrays in the
    OneDimensionalArray class. Not certain what the OP is trying to do. The title doesn't make much sense either.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Learning to use the Arrays class, object oriented style

    However, for the original post, that Arrays class had a valid constructor.

    The later code showed us some import statements, including:
    Java Code:
    import java.util.Arrays;
    I suspect that was always in there, which explains that original error.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Re: Learning to use the Arrays class, object oriented style

    Quote Originally Posted by jim829 View Post
    Regarding the Arrays class, it is the first class shown in his code. Then later the OP changed it to Array but still tried to instantiate Arrays in the
    OneDimensionalArray class. Not certain what the OP is trying to do. The title doesn't make much sense either.
    Eventually, I was forced to change the name of my class to Array, so that it wouldn't conflict with the Arrays utility class in the code. If you recall from my "How do I get sub string indexes to change without hard-coding them?" thread, you helped me develop code that instantiated a StringBuilder object, which eventually allowed this to work:
    Java Code:
    StringBuilder ccNumber = new StringBuilder(16);
            StringAppender.creditCard(ccNumber);
            StringAppender.addDashes(ccNumber);
            StringAppender.removeDashes(ccNumber);
    Now, I'm trying to achieve something similar with an Array object (don't confuse "Array object" with "an object defined in Array.java", I'm just saying I want to somehow create an array that's an object, which I've defined in OneDimensionalArray.java). The reason I've implemented the Arrays utility class in the first place is because I'm planning to use the sort and binarySearch methods of the Arrays utility class. I already have called the Arrays utility class's sort method on line 29 of OneDimensionalArray.java in my numSort method:
    Java Code:
    Arrays.sort(numbersAndStuff);
    That is what's making it difficult to instantiate a OneDimensionalArray.java object in the main method of the Array.java class. At this point, I'm unsure whether the problem lies in the syntax of my object instantiation on line #8 of Array.java:
    Java Code:
    OneDimensionalArray numbersAndStuff = new OneDimensionalArray();
    or on lines 16 and 28 of OneDimensionalArray.java:
    Java Code:
    public static int[] fillArray(int[] numbersAndStuff){
    
    public static int[] numSort(int[] numbersAndStuff){
    Is "int[]" even a valid return type?

    EDIT: it should be noted that I'm not trying to create an array of objects. Rather, I want to learn how to pass around an integer array as an object.
    Last edited by Sam_JavaTheHut5580; 07-13-2016 at 10:19 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Learning to use the Arrays class, object oriented style

    Is "int[]" even a valid return type?
    Yes a method can return an array of int: int[]
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Re: Learning to use the Arrays class, object oriented style

    Quote Originally Posted by Norm View Post
    Yes a method can return an array of int: int[]
    I thought so. Here's my updated code once again (as always, the newest errors are in the code comments):
    Array.java:
    Java Code:
    package Arrays01;
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class Array {
    	public static void main(String[] args){
    		
    		OneDimensionalArray numbersAndStuff = new OneDimensionalArray();//error: The constructor 
    		//OneDimensionalArray() is undefined
    		
    		OneDimensionalArray.fillArray(numbersAndStuff);//error: The method fillArray(int[]) in 
    		//the type OneDimensionalArray is not applicable for the arguments (OneDimensionalArray)		
    		
    		OneDimensionalArray.numSort(numbersAndStuff);//error: The method numSort(int[]) in 
    		//the type OneDimensionalArray is not applicable for the arguments (OneDimensionalArray)
    	}
    }
    OneDimensionalArray.java:
    Java Code:
    package Arrays01;
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class OneDimensionalArray {
    	
    	//instance variables:
    		//private Arrays numbersAndStuff;
    		private int[] numbersAndStuff;
    		
    		//overload constructor:
    		//public OneDimensionalArray(Arrays numbersAndStuff)
    		public OneDimensionalArray(int[] numbersAndStuff)
    		{
    			this.numbersAndStuff = numbersAndStuff;
    		}
    		
    	
    	public static int[] fillArray(int[] numbersAndStuff){
    		Scanner nums = new Scanner(System.in);
    		int currentNum = 0;
    		for(int i = 0; i < numbersAndStuff.length; i++){
    			currentNum++;
    			System.out.println("Enter integer for number [" + currentNum + "] of 10: ");
    			numbersAndStuff[i] = nums.nextInt();
    		}
    		nums.close();
    		return numbersAndStuff;
    	}
    		
    	public static int[] numSort(int[] numbersAndStuff){
    		Arrays.sort(numbersAndStuff);
    		return numbersAndStuff;
    	}	
    }//end of OneDimensionalArray class body
    What is the problem?!

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Learning to use the Arrays class, object oriented style

    The constructor OneDimensionalArray() is undefined
    The compiler can not find that constructor.

    not applicable for the arguments (OneDimensionalArray)
    The method does not take an instance of that class for an arg. See the method's definition for what args it requires.

    See the tutorial about using methods: Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Learning to use the Arrays class, object oriented style

    First off, is your OneDimensionalArray supposed to be sorting the int[] that is passed in in the constructor?
    If so then remove the parameters from the methods, and remove the static from them as well.

    You will then have a class that has an int[] attribute.
    You can call fill() on that class and it will populate the attribute with random numbers and return it.
    You can call sort() on that class and it will order that attribute and return it.

    When you create a new instance of that class you will need to pass in an int[] as that is what the constructor expects.
    Java Code:
    int[] numbersAndStuff = new int[..somenumber..];
    OneDimensionalArray oneDArray = new OneDimensionalArray(numbersAndStuff);
    Note that I renamed the variable used for the OneDimensionalArray as it was bloody confusing (as I said earlier).

    This is assuming this is what you actually want the class to do.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Re: Learning to use the Arrays class, object oriented style

    Quote Originally Posted by Tolls View Post
    First off, is your OneDimensionalArray supposed to be sorting the int[] that is passed in in the constructor?
    If so then remove the parameters from the methods, and remove the static from them as well.
    It's a little off topic, but check out the following two classes:
    Pokemon.java:
    Java Code:
    package SamPokemon.business;
    import java.text.NumberFormat;
    
    public class Pokemon {
    
    	//instance variables:
    	private String pokemonName;
    	private String pokemonType;
    	private double pokemonPrice;
    	
    	//default constructor:
    	public Pokemon(){
    		this("", "", 0.0);
    	}
    	
    	//overload constructor:
    	public Pokemon(String pokemonName, String pokemonType, double pokemonPrice){
    		this.pokemonName = pokemonName;
    		this.pokemonType = pokemonType;
    		this.pokemonPrice = pokemonPrice;
    	}
    	
    	//set and get methods:
    	public void setPokemonName(String name){
    		pokemonName = name;
    	}
    	public String getPokemonName(){
    		return pokemonName;
    	}
    	
    	public void setPokemonType(String type){
    		pokemonType = type;
    	}
    	public String getPokemonType(){
    		return pokemonType;
    	}
    	
    	public void setPokemonPrice(double price){
    		pokemonPrice = price;
    	}
    	public double getPokemonPrice(){
    		return pokemonPrice;
    	}
    	
    	public String getPokemonPriceFormatted(){
    		NumberFormat currency = NumberFormat.getCurrencyInstance();
    		String priceFormatted = currency.format(pokemonPrice);
    		return priceFormatted;
    	}
    }
    PokemonApp.java:
    Java Code:
    package SamPokemon.ui;
    import java.util.Scanner;
    
    import SamPokemon.business.Pokemon;
    import SamPokemon.business.PokemonItemTotal;
    import SamPokemon.db.PokemonDB;
    
    public class PokemonApp {
    
    	public static void main(String args[]){
    		System.out.println("Welcome to the pokedex.");
    		System.out.println();
    		
    		Scanner sc = new Scanner(System.in);
    		String choice = "y";
    		while(choice.equalsIgnoreCase("y")){
    			System.out.print("Enter the name of the pokemon you're looking for: ");			
    			String whoIsThatPokemon = sc.nextLine();
    			
    			System.out.print("Enter how many " + whoIsThatPokemon + " you wish to purchase: ");
    			int quantity = Integer.parseInt(sc.nextLine());
    			
    			//get the Pokemon object:
    			Pokemon pokemon = PokemonDB.getPokemon(whoIsThatPokemon);//static method call
    			
    			//PokemonDB db = new PokemonDB();
    			//Pokemon pokemon = db.getPokemon(whoIsThatPokemon);//instance method call
    			
    			//create the PokemonItemTotal object:
    			PokemonItemTotal pokemonItemTotal = new PokemonItemTotal(pokemon, quantity);			
    			
    			String message = "\nPOKEMON\n" +
    			"Pokemon: " + pokemon.getPokemonName() + "\n" +
    			"Type: " + pokemon.getPokemonType() + "\n" +
    			"Price: " + pokemon.getPokemonPriceFormatted() + "\n" +
    			"Quantity: " + pokemonItemTotal.getQuantity() + "\n" +
    			"Total: " + pokemonItemTotal.getTotalFormatted() + "\n";
    			
    			System.out.println(message);
    			
    			System.out.println("Do you want to continue?");
    			choice = sc.nextLine();
    			System.out.println();
    		}//end of while loop
    		sc.close();
    		System.out.println("Bye!");
    	}//end of main method
    }
    That, my friend, is the only kind of object oriented application the book showed me how to write so far. It uses primitive datatype instance variables, a default constructor, an overload constructor, and getters and setters. The book walked me through how to do a similar program beautifully. All along, I've been trying to achieve something similar with more advanced datatypes in hopes of deepening my understanding of object oriented programming. You guys helped me make that happen with my StringBuilder program, and now I'm trying to make it happen with object oriented arrays. Now back to my array class topic:

    Quote Originally Posted by Tolls View Post
    When you create a new instance of that class you will need to pass in an int[] as that is what the constructor expects.
    Java Code:
    int[] numbersAndStuff = new int[..somenumber..];
    OneDimensionalArray oneDArray = new OneDimensionalArray(numbersAndStuff);
    Well, that got rid of the red in Eclipse, but now my program terminated early with thrown exceptions. First, here's the code:

    Array.java:
    Java Code:
    public class Array {
    	public static void main(String[] args){
    		
    		OneDimensionalArray.bodyCount();
    	
    		int[] numbersAndStuff = new int[10];//is hardcoding the array size here one of the causes of the thrown exceptions?
    		OneDimensionalArray oneDArray = new OneDimensionalArray(numbersAndStuff);//The value of the local variable oneDArray is not used
    		
    		OneDimensionalArray.fillArray(numbersAndStuff);		
    		
    		OneDimensionalArray.numSort(numbersAndStuff);
    	}
    }
    OneDimensionalArray.java:
    Java Code:
    public class OneDimensionalArray {
    	
    	//instance variables:
    		//private Arrays numbersAndStuff;
    		private int[] numbersAndStuff;//The value of the field OneDimensionalArray.numbersAndStuff is not used
    		
    		//overload constructor:
    		//public OneDimensionalArray(Arrays numbersAndStuff)
    		public OneDimensionalArray(int[] numbersAndStuff)
    		{
    			this.numbersAndStuff = numbersAndStuff;
    		}
    		
    		public static void bodyCount(){
    			Scanner x = new Scanner(System.in);//Resource leak: 'x' is never closed
    			System.out.println("How many people did you kill today?");
    			int bodyCount = Integer.parseInt(x.nextLine());
    			String names[] = new String[bodyCount];
    			System.out.println("Who were the people that you killed?");
    			//since names is a field and not a method, empty parentheses 
    			//after length are not needed here:
    			int person = 0;
    			Scanner y = new Scanner(System.in);
    			for(int i = 0; i < names.length; i++){
    				person++;
    				System.out.print("Person " + person + ": ");
    				names[i] = y.nextLine();
    			}
    			y.close();
    			System.out.println("Today, the people you killed were:");
    			for(int i = 0; i < names.length-1; i++){
    				System.out.print(names[i] + ", ");
    			}
    			int lastPerson = names.length;
    			System.out.print("and " + names[lastPerson-1] + ".");
    			System.out.println();
    		}	
    		
    	public static int[] fillArray(int[] numbersAndStuff){
    		Scanner nums = new Scanner(System.in);
    		int currentNum = 0;
    		for(int i = 0; i < numbersAndStuff.length; i++){
    			currentNum++;
    			System.out.println("Enter integer for number " + currentNum + " of 10: ");
    			numbersAndStuff[i] = nums.nextInt();//program apparently crashes here before completing first for loop iteration
    		}
    		nums.close();
    		return numbersAndStuff;
    	}
    	
    	public static int[] numSort(int[] numbersAndStuff){
    		Arrays.sort(numbersAndStuff);
    		return numbersAndStuff;
    	}	
    }//end of OneDimensionalArray class body
    And now for the program crash exception output from console:
    Learning to use the Arrays class, object oriented style-arrays01_thrownexception.jpg
    It should be noted that, in the code I posted here to save space, the exception referring to line 50 in OneDimensionalArray.java is actually referring to line 45, and the exception referring to line 78 in Array.java is actually referring to line 9.

    Also, as I commented in the code, the program isn't even using my constructor in OneDimensionalArray.java, nor is it using the oneDArray object instantiation on line 7 in Array.java.

    So what's going on now? What's causing my program to crash and throw exceptions? They're not as easy for me to pinpoint, since they're runtime errors.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Learning to use the Arrays class, object oriented style

    If you're closing a Scanner, it also closes the wrapped InputStream and it can't be read from anymore (see line #29). Use a single Scanner throughout your code for the System.in InputStream.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  17. #17
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Re: Learning to use the Arrays class, object oriented style

    My program is terminating early when I run it:
    OneDimensionalArray.java:
    Java Code:
    public class OneDimensionalArray {
    	
    	//instance variables:
    		//private Arrays numbersAndStuff;
    		private int[] numbersAndStuff;//The value of the field OneDimensionalArray.numbersAndStuff is not used
    		
    		//overload constructor:
    		//public OneDimensionalArray(Arrays numbersAndStuff)
    		public OneDimensionalArray(int[] numbersAndStuff)
    		{
    			this.numbersAndStuff = numbersAndStuff;
    		}
    		
    		public static void bodyCount(){
    			Scanner x = new Scanner(System.in);//Resource leak: 'x' is never closed
    			System.out.println("How many people did you kill today?");
    			int bodyCount = Integer.parseInt(x.nextLine());
    			String names[] = new String[bodyCount];
    			System.out.println("Who were the people that you killed?");
    			//since names is a field and not a method, empty parentheses 
    			//after length are not needed here:
    			int person = 0;
    			Scanner y = new Scanner(System.in);
    			for(int i = 0; i < names.length; i++){
    				person++;
    				System.out.print("Person " + person + ": ");
    				names[i] = y.nextLine();
    			}
    			//y.close();
    			System.out.println("Today, the people you killed were:");
    			for(int i = 0; i < names.length-1; i++){
    				System.out.print(names[i] + ", ");
    			}
    			int lastPerson = names.length;
    			System.out.print("and " + names[lastPerson-1] + ".");
    			System.out.println();
    		}	
    		
    	public static int[] fillArray(int[] numbersAndStuff){
    		Scanner nums = new Scanner(System.in);
    		int currentNum = 0;
    		for(int i = 0; i < numbersAndStuff.length; i++){
    			currentNum++;
    			System.out.print("Enter integer for number " + currentNum + " of 10: ");
    			numbersAndStuff[i] = nums.nextInt();
    		}
    		nums.close();
    		return numbersAndStuff;
    	}
    	
    	public static int[] numSort(int[] numbersAndStuff){
    		Arrays.sort(numbersAndStuff);
    		return numbersAndStuff;
    	}	
    }//end of OneDimensionalArray class body
    Array.java:
    Java Code:
    public class Array {
    	public static void main(String[] args){
    		
    		OneDimensionalArray.bodyCount();
    	
    		int[] numbersAndStuff = new int[10];
    		OneDimensionalArray oneDArray = new OneDimensionalArray(numbersAndStuff);
    		
    		oneDArray.fillArray(numbersAndStuff);
    		System.out.println("Our array will now be sorted:");//program currently ends here
    		oneDArray.numSort(numbersAndStuff);		
    	}
    }
    Here is a console output sample:
    Learning to use the Arrays class, object oriented style-arrays01_terminatingearly.jpg
    So why won't my program go into the numSort method?

  18. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Learning to use the Arrays class, object oriented style

    What makes you think it isn't being sorted? You call sort and then do nothing with the array. Also, your numSort method
    returns an int[] but you don't use it. Besides, Arrays.sort modifies the passed array so you don't need to return anything.
    In fact, why create the numSort method at all?

    Regards,
    Jim
    Last edited by jim829; 07-24-2016 at 04:30 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Learning to use the Arrays class, object oriented style

    @OP: why are you ignoring my tip about using a single Scanner throughout the entire code? If the InputStream were buffered you'd been in more trouble than you'd want to be; also think before you post here and use the cheapest debugger avallable: System.out.println( ... ) and sprinkle it in your code wherever you need/want it.

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Learning to use the Arrays class, object oriented style

    Quote Originally Posted by Sam_JavaTheHut5580 View Post
    That, my friend, is the only kind of object oriented application the book showed me how to write so far. It uses primitive datatype instance variables, a default constructor, an overload constructor, and getters and setters. The book walked me through how to do a similar program beautifully. All along, I've been trying to achieve something similar with more advanced datatypes in hopes of deepening my understanding of object oriented programming. You guys helped me make that happen with my StringBuilder program, and now I'm trying to make it happen with object oriented arrays.
    Compare the Pokemon class to your OneDimensionaArray class.
    Do you see any 'static' keywords in the Pokemon class?
    That would be my first thing to fix.
    At the moment your OneDArray class is not object oriented.
    It consists of static methods.

    So if you want to learn OO and working with objects then you need to try and avoid sticking static in everywhere.
    And when you remove the 'static' from the methods, then you also should remove the parameter from them, as I suspect you really want to manipulate the array that's passed in in the constructor.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 05-24-2014, 08:26 PM
  2. new to object oriented programming and arrays
    By maximumammo in forum New To Java
    Replies: 11
    Last Post: 04-22-2012, 08:25 PM
  3. New to object-oriented
    By fluteattack in forum New To Java
    Replies: 6
    Last Post: 03-28-2011, 01:43 PM
  4. Object Oriented
    By GraemeH in forum New To Java
    Replies: 3
    Last Post: 03-29-2009, 07:50 PM
  5. Object- Oriented guide?
    By sciguy77 in forum New To Java
    Replies: 4
    Last Post: 01-18-2009, 03:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •