Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Methods in a class

  1. #1
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Methods in a class

    Hey!

    I got this class with some static methods in it from an assignment that i am doing atm.
    One method should act as the following : Method void replaceAll(int[] arr, int old, int nw), replacing all occurrences of old with nw in arr.

    However i feel lost.. does anyone know what they mean? And if so then how should i approach this?


    Here is the code:
    import java.util.Scanner;
    //Forbidden to use array classes. 
    public class engV {
    	static final int[] arr = { 3, 4, 5, 6, 7 };
    	// Made this a static and a final so that it won't change and so that everything
    	// can access it!
     
    	public static void main(String[] args) {
     
    		Scanner in = new Scanner(System.in);
     
    		System.out.println(sum(arr)); // For sum method. It takes all the values of arr and adds them together.
    		System.out.println();
    		System.out.println();
    		System.out.println(tostring(arr)); // Prints out the numbers from the array.
    		System.out.println();
    		System.out.println();
     
    		// Adds numbers to each element.
    		System.out.println("Write a number that you would like to add to each element in the array :");
    		int input = in.nextInt();
    		System.out.print("The new numbers are : ");
    		addN(arr, input); // Takes return from addN method.
     
    		System.out.println();
    		System.out.println();
    		System.out.println();
    		System.out.print("Reverse order of array : ");
    		reverse(arr); // Writes the reverse order of the array.
    		System.out.println("\n");
    		hasN(arr, 4); // Tests to see if the number within the parentheses, in this case it checks if
    						// 4 is within the array.
    		in.close();
     
    	}
     
    	public static int sum(int[] arr) {
     
    		int n = 0;
    		for (int i : arr) {
    			n += i;
    		}
    		return n;
    	}
     
    	public static String tostring(int[] arr) {
    		String strk = " ";
    		int i = 0;
    		String st = null;
     
    		for (i = 0; i < arr.length; i++) {
    			st = Integer.toString(arr[i]);
    			System.out.print(st + strk);
    			if (i >= arr.length) {
    				break;
    			}
    		}
     
    		return strk;
    	}
     
    	public static int[] addN(int[] arr, int n) {
     
    		String strK = " ";
    		int results[] = null;
    		int k = 0;
    		for (k = 0; k < arr.length; k++) {
    			System.out.print(arr[k] + n + strK);
    		}
    		return results;
    	}
     
    	public static int[] reverse(int[] arr) {
     
    		int counter = 0;
    		String str = " ";
    		for (counter = arr.length - 1; counter >= 0; counter--) {
    			System.out.print(arr[counter] + str);
     
    		}
    		return arr;
     
    	}
     
    	public static boolean hasN(int[] arr, int n) {
     
    		boolean found = false;
    		for (int i = 0; !found && (i < arr.length); i++) {
     
    			found = (arr[i] == n);
    		}
     
    		if (found == true) {
    			System.out.println("Yes, " + n + " does exist in the array!");
     
    		} else {
    			System.out.println("No, " + n + " does not exist in the array!");
     
    		}
    		return found;
     
    	}
     
    	public static void replaceAll(int[] arr, int old, int nw) {
     
    		//Here i get lost... 
     
    	}
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,318
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default Re: Methods in a class

    what they mean?
    I would assume they mean
    given array that contains 1,2,3,4,2
    replacing all 2 with 99 would change the arrays contents to 1,99,3,4,99

    Have you tested the other methods? Do they give the correct results?
    It's usually better to work on ONE method at a time. Design it, code it, compile and execute for testing.
    Continue those steps until the results are as desired BEFORE working on the next method.

    final so that it won't change
    That's only true for the variable: arr. The contents of the array can be changed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Methods in a class

    That would be my take as well.

    Regards,
    Jim

  4. #4
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Methods in a class

    Quote Originally Posted by Norm View Post
    I would assume they mean
    given array that contains 1,2,3,4,2
    replacing all 2 with 99 would change the arrays contents to 1,99,3,4,99

    Have you tested the other methods? Do they give the correct results?
    It's usually better to work on ONE method at a time. Design it, code it, compile and execute for testing.
    Continue those steps until the results are as desired BEFORE working on the next method.

    Quote Originally Posted by jim829 View Post
    That would be my take as well.

    Regards,
    Jim


    Yes everything works so far. Well that does sound about right.
    Thanks guys!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,318
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default Re: Methods in a class

    everything works so far.
    Have you looked at the value returned by hasN? Change the call to hasN to save the value that is returned and then print that with a message showing what the expected value is and what the returned value was.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Methods in a class

    Quote Originally Posted by Norm View Post
    Have you looked at the value returned by hasN? Change the call to hasN to save the value that is returned and then print that with a message showing what the expected value is and what the returned value was.
    Doesn't it work for you?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,318
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default Re: Methods in a class

    The code does not save the value returned by hasN. The print outs should be after the call to hasN, not as part of the method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. multiple methods in one class
    By wamidh in forum Java Theory & Questions
    Replies: 6
    Last Post: September 16th, 2014, 03:44 PM
  2. [SOLVED] Write a class named Calculator add four methods to that class
    By zahid32 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 30th, 2014, 06:06 AM
  3. Need help finishing class methods
    By R2B Boondocks in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 24th, 2013, 11:13 AM
  4. Help requested - testing a class with a tester class, no methods allowed.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 09:40 PM
  5. [SOLVED] Help with Class Methods(Probably simple)
    By ShakeyJakey in forum Object Oriented Programming
    Replies: 9
    Last Post: May 27th, 2010, 09:53 AM