Computer Science, asked by tanishq7475, 3 months ago

a. State the method that: :
i. Converts a String to primitive float type.
ii. Determines if the specified character is an uppercase character.
b. Differentiate between Searching and Sorting.

Answers

Answered by Equestriadash
107

a.

(i) A method that converts a string to float type:

  • float()

You can simply call the float() function to a variable that holds a string value, or mention the string in the brackets of the function.

Example:

>>> s = "123"

>>> float(s)

123.0

>>> float("145")

145.0

It's important to keep in mind that a string containing letters/special characters will not change into a float type and will result in an error.

Example:

>>> s = "Hello"

>>> float(s)

Traceback (most recent call last):

 File "<pyshell#3>", line 1, in <module>

   float(s)

ValueError: could not convert string to float: 'Hello'

(ii) A method that determines if the specified character is an uppercase character or not:

  • isupper()

You can check if a string has a letter in uppercase form by simply storing it into a variable and testing it using the following syntax:

string_name.isupper()

Or you can directly give the string by using this syntax as well:

string.isupper()

If it returns True, the character(s) is/are in uppercase form. If it returns False, it is/are in lowercase form.

Keep in mind, that if a string has more than 1 letter, it will return True only if all the letters are in uppercase form.

Example:

>>> s = "Hello"

>>> s.isupper()

False

>>> "HELLO".isupper()

True

b. Differenciating between searching and sorting.

  • Searching is the process of traversing through a string/list/dictionary, ... etc to get/find a specific element. The most common method(s) used for such purposes include using slice commands and loops.
  • Sorting is the process of arranging elements in the ascending/descending order. Python has its own function(s) for it, sort() and sorted().

Anonymous: Exemplary! ( ꈍᴗꈍ)
Equestriadash: Thank you! ^_^"
Answered by BrainlyProgrammer
6

Question a i.

//This is a JAVA Programming

public class Program

{

public static void main(String[] args) {

String str= "123";

float p= Float.parseFloat(str);

System.out.println("Given String:"+str);

System.out.println("Converting to float data type... ... "+p);

}

}

REMEMBER:-

  • If you convert alphanumeric(words or characters) string then it will show NumberFormatException.
  • Float.parseFloat() is used for converting string to float data type
  • Similarly, you can use Integer.parseInt() to convert string to int data type

Question a ii.

public class Program

{

public static void main(String[] args) {

char ch='A'; //Storing upper case character

char ch1='a'; //Storing Lower case character

System.out.println(Character.isUpperCase(ch));

System.out.println(Character.isUpperCase(ch1));

}

}

Question b.

Searching

  • When we want to search a given number in an array then we use Searching technique.
  • There are two techniques of searching
  • Binary Searching:- Most popular method of searching a number in an array. It works only on sorted array. It divides the array into two parts and check if the number is greater than the middle ellement of an array if yes then it moves tonwards the right else it moves towards the left. (Assume that array is sorted in ascending order)
  • Linear Searching:- This can work even if the array is not sorted. it checks the number with the array elements starting from left side /right side . As soon as the number is found.. Search Successful!

Sorting

  • When we want to arrange an array in ascending/descending order we call it sorting.
  • There are two kinds of sorting
  • Bubble Sorting :- Popular method of sorting.
  • Selection Sorting

Anonymous: Awesome!
Skyllen: Great..
Similar questions