Jump to content

Java homework help

- - - - -

  • Please log in to reply
5 replies to this topic

#1
bkim33

bkim33

    Newbie

  • Members
  • Pip
  • 4 posts
can anyone help me with my homework assignment. It is a simple problem but i am having a hard time learning java. I need to create a boolean method that returns true if all chars at source are found in target..

public class Wa {


	

	//

	// isLike

	// Returns true if all chars in source are found in target.

	//

	public static boolean isLike(String source, String target) {

		for (int x = 0; x < source.length(); x++) {

			

		}

		

		

		

	}

	

	

	//

	// isInteger

	// Returns true if string represents an integer

	// (integer variable max and min values do not need to be considered)

	//

	// Examples of "good" integer strings:

	//      "123456789"

	//      "00023"         allow leading zeros

	//      "  87   "       allow leading and trailing spaces

	//

	// Examples of "bad" integer strings:	

	//      "34*$D3456(9"   bad symbols

	//      "23.6"          bad symbols

	//      "45 454"        string should be one integer

	//

	public static boolean isInteger(String source) {


		

		

		

	}

	

	//

	// toInteger

	// Converts a string to an integer value.

	// If string is an invalid integer, return 0.

	// (integer variable max and min values do not need to be considered)

	//

	public static int toInteger(String source) {


		

		

	}

	

	//

	// isDouble

	// Returns true if string represents a double

	//

	// Examples of "good" integer strings:

	//      "12.7"

	//      "98."

	//      "00023.00"         allow leading zeros

	//      "  87.7   "        allow leading and trailing spaces

	//

	// Examples of "bad" integer strings:	

	//      "34*8D3.456$(9"   bad symbols

	//      "2.3.6"          too many decimals

	//      "45 4.54"        string should be one double

	//

	public static boolean isDouble(String source) {

		

		

		

		

	}

	

	

	// main

	public static void main(String args[]) {

		// test your methods

		

	}


}


how do i tackle this? Do i have to loop the two strings twice then store each letter as a variable and see if the two variables are equal?

any help would be greatly appreciated
bkim33 is online now Edit/Delete Message

#2
cycronica

cycronica

    Newbie

  • Members
  • Pip
  • 5 posts
If I understand you correctly, you want to check that all the characters found in source are found in target.
A simple way to solve this is to sort both strings and then check if they equal each other.
Sorting a string can be done in this way: Java - Sorting a String
Checking whether to String objects are equals is simply enough: str1.equals(str2);

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
The isLike method is allready pretty complete. You just need to know 2 string methods.
Which are:
  • boolean : contains(String) eg: if( target.contains("X") )
  • charAt(int) in conjunction with x. eg: source.charAt(x);


#4
cycronica

cycronica

    Newbie

  • Members
  • Pip
  • 5 posts
I would disagree there wim DC,
as contains only returns true or false you would not be able to see if there are more letters of the same one.
To use contains you would then have to remove the one you found so that a possible future contains on the same letter would find a new one.
For example:
source: babe
target: bae
Running target.contains('b'); twice will be true both times even though there are only one 'b'.
Another point bkim33 is to first check the length of the String objects first.
If the lengths are not the same, they cannot hold the same characters.

Please do correct me if I have understood this task incorrectly.

#5
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I have to disagree with you cycronica. The lengths should not have to be equal.
The OP is testing similarity, not equality.

Example:

Source: mbc;

target: abcdefghijklmnopqrstuvwxyz;

// **Returns true if all chars in source are found in target.

//All characters from source are in target, therefore true;

//Not all characters from target are in source though; but this isn't the case



#6
cycronica

cycronica

    Newbie

  • Members
  • Pip
  • 5 posts
Thank you for pointing that out for me,
must have read through too fast :D
Going through the source and using contains should do the trick then.
If one letter is not to find, return false.
Sorry for the confusion.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users