Question

Lab 6                                    Given the String String str = “Zhrx.aghkio.G

Lab 6                                   

Given the String

String str = “Zhrx.aghkio.Gko123”;

(a) Write Java code using the String class methods indexOf and substring to extract the three string surrounded by the periods. In other words

  1. String s1 will contain Zhrx,
  2. String s2 will contain aghkio and
  3. String s3 will contain Gko123.

(b) Write a method of the form

public static String minStr(String str1, String str2)

which returns the string which is “smaller” based on the decimal values of the characters in the string. Thus, Zaa would be “smaller” than bxy because the character Z has a lower decimal values than b. Use the String method compareTo as part of your code.

(c) In main, print the value returned by

        minStr(minStr(s1,s2),s3)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package com.abc;

public class StringDemo {

   public static void main(String[] args) {
       String str = "Zhrx.aghkio.Gko123";
       String s1=str.substring(0,4);
       String s2=str.substring(5,11);
       String s3=str.substring(12,str.length());
       System.out.println(s1);
       System.out.println(s2);
       System.out.println(s3);
       String finalvalue=StringDemo.minStr(s1, s2);
       String finalvalue1=StringDemo.minStr(finalvalue, s3);
       System.out.println(finalvalue);
       System.out.println(finalvalue1);
   }
   public static String minStr(String str1, String str2) {
      

   int var1 = str1.compareTo( str2 );
   System.out.println("str1 & str2 comparison: "+var1);
   return str2;
   }

}

Add a comment
Know the answer?
Add Answer to:
Lab 6                                    Given the String String str = “Zhrx.aghkio.G
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java beginners: Show how to invoke the method grunf so that it will return the String...

    Java beginners: Show how to invoke the method grunf so that it will return the String HOPE.         public static String grunf(String s1, String s2, String s3) {                 String s = s1.substring(1,2);                 if (s1.length()>s2.length() && s2.compareTo(s3)<0 && s3.indexOf(s)>0)                         return s1.substring(0,1) + s2.substring(1);                 else                         return s1.substring(0,1) + s2.substring(0,1) + s1.substring(0,1);         }

  • Can someone help me out with this? You are given a program that receives four lines...

    Can someone help me out with this? You are given a program that receives four lines in the below format, and stores them in str1, str2, str3, and num1. This is not a very long sentence. is long 4 Expand this program to: Write an if-elseif-else statement to print this line only if num1 is higher than 0: "Num1 is higher than 0!" print this line only if num1 is 0: "Num1 equals to 0!" And otherwise, print: ""Num1 is...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • Write Java Programs to perform the following: 1. To create a new String object 2. To...

    Write Java Programs to perform the following: 1. To create a new String object 2. To find the length of the string 3. Concatenation operator 4. To display the content in String array 5. A complete Java Program that incorporates the following String methods taught in class a. toUpperCase()) b. to LowerCase()) C. replace('0','A')) d. s1.length() e. compareTo(s1)) methods taught in class a. toUpperCase()) b. to LowerCase()) C. replace('0','A')) d. s1.length()) e. compareTo(s1)) f. 51.substring(4)) g. s2.substring(2,10)) h. s1.indexOf('g')) i....

  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

  • Write a Java program for all items in Question Use System.out.println() for each resulting string. Let...

    Write a Java program for all items in Question Use System.out.println() for each resulting string. Let s1 be " Welcome " and s2 be " welcome ". Write the code for the following statements: a. Check whether s1 is equal to s2 and assign the result to a Boolean variable isEqual. b. Check whether s1 is equal to s2, ignoring case, and assign the result to a Boolean variable isEqual. c. Compare s1 with s2 and assign the result to...

  • From the Tony Gaddis text, the chapter on C-String and Class String: String Length: Write a...

    From the Tony Gaddis text, the chapter on C-String and Class String: String Length: Write a Function that passes in a C-String and using a pointer determine the number of chars in the string.                                           Data:   “This is a test string for string length” Prt String Backwards: Write a Function that passes in a C-String and prints the string backwards.      Data: “This is a test string for string backwards” replaceSubstring: Write a Function that accepts three C-Strings – str1,...

  • 8.4 in python function that checks whether a string is a valid password. Suppose the pas...

    8.4 in python function that checks whether a string is a valid password. Suppose the pas rules are as follows: . A password must have at least eight characters - A password must consist of only letters and digits. ■ A password must contain at least two digits. Write a program that prompts the user to enter a password and displays valid password if the rules are followed or invalid password otherwise (Occurrences of a specified character) Write a function...

  • Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray();...

    Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray(); char[] reverse = new char[original.length]; for(int i =0; i<s.length(); i++){ reverse[i] = original[original.length-1-i]; } return new String(reverse); } /**  * Takes in a string containing a first, middle and last name in that order.  * For example Amith Mamidi Reddy to A. M. Reddy.  * If there are not three words in the string then the method will return null  * @param name in...

  • Write a class StringsAndThings. The class has one instance variable of type String and a constructor...

    Write a class StringsAndThings. The class has one instance variable of type String and a constructor with a parameter to initialize the instance variable. The parameter could contain any characters, including letters, digits, spaces, special characters (+, -, $, @,...), and punctuation marks. Write methods: countNonLetters - that will count how many of the characters in the string are not letters. You can use Character's isLetter method. moreVowels - which returns true if the String parameter has more vowels than...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT