Jump to content

Java Program Help. Arrays

SlaughterApollo

This is the problem I have word for word.

The lab is called P_NumCheck. It requires you to write two methods. The first is buildBoolean. This method receives an interger value and an array of integers. It builds a boolean array of the same size as the integer array. The elements in the boolean array are determined by whether or not the corresponding values in the integer array are bigger than the integer value sent into the array. If they are bigger than the integer true is assigned at that index in the boolean array. If the element is less than or equal to the integer value false is assigned.

 

The second method is called outputArray. It simply processes the boolean array and outputs T for all the true values and F for all the false values. If all of the values are true, it will output "All <num> true" instead the contents of the array, where num is the number of elements in the arrary. If all of the values are false it will output " All <num> false".

 

SAMPLE OUTPUT:

 

Enter number of elements in the array followed by the values with each separated by a space

5 6 7 8 9 10

Enter the integer number to compare them against:

8

Expected Output:

FFFTT

 

SAMPLE OUTPUT 2:

Enter number of elements in the array followed by the values with each separated by a space

4 7 12 9 11

Enter the integer number to compare them against:

2
Expected Output:
All 4 true
 
 
We were provided with a main method to use on the program which is as follows
public static void main(String[] args){                Scanner s = new Scanner(System.in);		System.out.println("Enter number of elements in the array followed by the values with each separated by a space");		int size = s.nextInt();		int[] nums = new int[size];		for(int x = 0; x < nums.length; x++)			nums[x] = s.nextInt();		System.out.println("Enter the integer number to compare array against:");		int value = s.nextInt();		boolean[] checks = buildBoolean(value, nums);		outputArray(checks);}

And this is the program that I have written

import java.util.Scanner;public class P_NumChecks {	public static void main(String[] args){        Scanner s = new Scanner(System.in);System.out.println("Enter number of elements in the array followed by the values with each separated by a space");int size = s.nextInt();int[] nums = new int[size];for(int x = 0; x < nums.length; x++)	nums[x] = s.nextInt();System.out.println("Enter the integer number to compare array against:");int value = s.nextInt();boolean[] checks = buildBoolean(value, nums);outputArray(checks, size);}		public static boolean[] buildBoolean(int value, int [] nums){		boolean [] tempArray = new boolean[nums.length];		for(int x = 0; x < nums.length; x++ ){			if(nums[x] > value){				tempArray[x] = true;			}			else if(nums[x] <= value){				tempArray[x] = false;			}		}		return tempArray;		}    public static void outputArray(boolean[] checks, int size) {    	int t = 0;    	int f = 0;    	System.out.println("Expected Output:");    	for(int x = 0; x < checks.length; x++){    		if(checks[x] = true){    			t++;    			if(t > size){    				System.out.println(" All " + (size) + " True");    			}    			if(checks[x] = false){    				f++;    				if(f > size){    					System.out.print(" All " + (size) + " false");    				}    				else if(f <= size){    					System.out.print("F");    				}    			}    			else if(t <= size){    				System.out.print("T");    			}    		}    		}    	}			}	

Clearly it doesn't work property. Please provide me with a working example and why it does not work. Try not to use any means of code that is too advanced compared to what you see above.

 

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not going to do your work for you, so I'll give you clues.

 

bugs at lines 35 and 40.

 

Just make your logic simpler from lines 34 to 51/52. Run one loop to check if there are both trues and falses or not and then print out the boolean array.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not going to do your work for you, so I'll give you clues.

 

bugs at lines 35 and 40.

 

Just make your logic simpler from lines 34 to 51/52. Run one loop to check if there are both trues and falses or not and then print out the boolean array.

I finally resolved it after completely rethinking it.

import java.util.Scanner;public class P_NumChecks {	public static void main(String[] args){        Scanner s = new Scanner(System.in);System.out.println("Enter number of elements in the array followed by the values with each separated by a space");int size = s.nextInt();int[] nums = new int[size];for(int x = 0; x < nums.length; x++)	nums[x] = s.nextInt();System.out.println("Enter the integer number to compare array against:");int value = s.nextInt();boolean[] checks = buildBoolean(value, nums);outputArray(checks, nums);}		private static void outputArray(boolean[] checks, int[] nums) {		String[] output = new String[nums.length];    	int t = 0;    	int f = 0;    	for(int y = 0; y < nums.length; y++){    	    	if(checks[y]){    		t++;    		output[y] = "T";    	}    	else{    		f++;    		output[y] = "F";    	}		}		if(t == nums.length) System.out.println("All " + nums.length + " true");			else if(f == nums.length) System.out.println("All " + nums.length + " false");				else{		for(int y = 0; y < nums.length; y++){			System.out.print(output[y]);					}			}			}	public static boolean[] buildBoolean(int value, int [] nums){	boolean[] temp = new boolean[nums.length];	//String[] output = new String[nums.length];	System.out.println("Expected Output:");	for(int y = 0; y < nums.length; y++){	if(nums[y] > value) temp[y] = true;	else temp[y] = false;	}	return temp;    	}}	
Link to comment
Share on other sites

Link to post
Share on other sites

 

I finally resolved it after completely rethinking it.

-snip-

If you resolved it, please mark the topic as resolved.

I might be wrong.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×