Question

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 lower than 0!"
  • Write another if-elseif-else statement to
    • if the length of str1 or str2 is lower than 1, print: "Error! Analysis is not possible!"
    • otherwise, if the length of the sentence is lower than 5 and str1 contains str2, print:"Barely a sentence but had the word!"
    • otherwise, if the length of the sentence is lower than 5 and str1 DOES NOT contain str2, print:"Barely a sentence and DID NOT have the word!"
    • otherwise, if the length of the sentence is between 5 (inclusive) and 20 (exclusive) and str1 contains str2, print:"A sentence and had the word!"
    • otherwise, if the length of the sentence is between 5 (inclusive) and 20 (exclusive) and str1 DOES NOT contain str2, print:"A sentence and DID NOT have the word!"
    • In any other case, prints: "Sentence is too long for analysis."
  • Prints the first and last character of str1 in the below format:
[first character][last character]
  • Search str1 for str2,

    • do nothing if it couldn't find it.
    • and print this line to the console if it could:"Found it!"
  • Compare str2 and str3 and

    • print str2 if str2 is larger than str3
    • print str3 if str2 is smaller than str3
    • print this line if str2 equals str1: "They are equal!"
  • Find the str2 in str1, and

    • if it could be found, print two separate strings, one string from the beginning of str1 to right before str2, and another one from the end of str2 until the end of the string.
    • Otherwise, do nothing.
    • For example, if the input is:
This is not a very long sentence.
er
long
4

The output will be:

This is not a v
y long sentence.

Below is the list of methods that you might need

  • str1.equals(str2)
  • str1.contains(str2)
  • str1.startsWith(str2) / str1.endsWith(str2)
  • str1.compareTo(str2)
  • str1.equalsIgnoreCase(str2)
  • str1.compareToIgnoreCase(str2)
  • str1.charAt(int);
  • str1.length()
  • str1.indexOf(char/String)
  • str1.replace(str2,str3)
  • str1.substring(firstIndex,secondIndex+1)
  • str1.concat(str2)

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String str1 = scnr.nextLine();
String str2 = scnr.nextLine();
String str3 = scnr.nextLine();
int num1 = scnr.nextInt();

// Write your code below.

}
}

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

JAVA CODE

============================================================================================

package hello;
import java.util.*;
public class Main {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner scnr = new Scanner(System.in);
       String str1 = scnr.nextLine();
       String str2 = scnr.nextLine();
       String str3 = scnr.nextLine();
       int num1 = scnr.nextInt();
      
       //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 lower than 0!"
       if(num1>0)
           System.out.println("Num1 is higher than 0!");
       else if(num1==0)
           System.out.println("Num1 equals to 0!");
       else
           System.out.println("Num1 is lower than 0!");
      
      
       if(str1.length()<0 || str2.length()<0)
           System.out.println( "Error! Analysis is not possible!");
       else if(str1.length()<5 && (str1.contains(str2)==true))
           System.out.println("Barely a sentence but had the word!");
       else if(str1.length()<5 && (str1.contains(str2)==false))
           System.out.println("Barely a sentence and DID NOT have the word!");
       else if(str1.length()<20 && (str1.contains(str2)==true))
           System.out.println("A sentence and had the word!");  
       else if(str1.length()<20 && (str1.contains(str2)==false))
       System.out.println("A sentence and DID NOT have the word!");
       else
           System.out.println("Sentence is too long for analysis.");
      
       //Prints the first and last character of str1 in the below format:
       //[first character][last character]
       System.out.println("["+str1.charAt(0)+"]["+str1.charAt(str1.length()-1)+"]");
      
       //Search str1 for str2
       if(str1.contains(str2))
           System.out.println("Found it!");
       //Compare str2 and str3
       if(str2.compareTo(str3)>0)
           System.out.println(str2);
       else if(str2.compareTo(str3)<0)
           System.out.println(str3);
       else
           System.out.println("They are equal!");
      
       //Find the str2 in str1
       String s[]=str1.split(str2);
       for(String k:s)
           System.out.println(k);

   }

}

============================================================================================

Output

Add a comment
Know the answer?
Add Answer to:
Can someone help me out with this? You are given a program that receives four lines...
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
  • 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,...

  • 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...

  • Please help!! These are the errors I'm getting ... string_2_set - << Back Log out 1-def...

    Please help!! These are the errors I'm getting ... string_2_set - << Back Log out 1-def string_2_set(str): 2 Given a string, split it into words (based on whitespace). For each word, remove the first and last letters from the word, and insert the pair (as a two-character word) into the set. d = set() #empty set str2 -str , split(" ") #split string with space first = str2[0][0] + str2[0] [len(str2[0])-1] #first pair if(len(str2[1]) > 1): If any word is...

  • Can someone help me to see what" wrong with my code?

    def cleanedup(s):  alphabet = 'abcdefghijklmnopqrstuvwxyz#'  cleantext = ''  for character in s.lower():    if character in alphabet:      cleantext += character    else:      cleantext += ' '  return cleantextlinecount=0maxlength=0with open('elon-musk.txt') as text:  for line in text:    linecount+=1    length=len(line.split())  if length>maxlength:     maxline=line     maxlength=length  print('number of tweets:',linecount)  print('Tweet with max number of words:', maxline)        tweets={}with open('elon-musk.txt') as text:    for line in text:        for word in cleanedup(line).split():       ...

  • In python, please help me fill in these functions given their limitations. Please use for loops....

    In python, please help me fill in these functions given their limitations. Please use for loops. def palindrome(x): """ For a given string, determine if the string is a palindrome. (The same forward and back) Input: String of any size (including empty) Return: Boolean (True if palindrome, False otherwise) Limitation: You must use a loop. Only function allowed to be used is len (if needed). Cannot use "in" besides "for var in container" """ pass def getCount(char, strng): """ Get...

  • CSC Hw Problems. Any help is appreciated I dont know where to start let alone what...

    CSC Hw Problems. Any help is appreciated I dont know where to start let alone what the answers are. Your assignment is to write your own version of some of the functions in the built-in <string.h> C library. As you write these functions, keep in mind that a string in C is represented as a char array, with the '\0' character at the end of the string. Therefore, when a string is passed as a parameter, the length of the...

  • Hi, it's C++ question. Only can use <iostream> and <fstream>libraries Please help me, thanks Question Description:...

    Hi, it's C++ question. Only can use <iostream> and <fstream>libraries Please help me, thanks Question Description: For this project you will write a program to: a) read-in the 10 first names from a file (the file is a priori given to have exactly 10 entries, of a maximum length of 8 letters each) into a 2-dimensional character array, b) output the names to the terminal with each one preceded by a number indicating its original order in the list, c)...

  • hey dear i just need help with update my code i have the hangman program i...

    hey dear i just need help with update my code i have the hangman program i just want to draw the body of hang man when the player lose every attempt program is going to draw the body of hang man until the player lose all his/her all attempts and hangman be hanged and show in the display you can write the program in java language: this is the code bellow: import java.util.Random; import java.util.Scanner; public class Hangmann { //...

  • This looks long but it is easy just having some trouble please help out thank you....

    This looks long but it is easy just having some trouble please help out thank you. Find and fix all syntax and semantic errors which prevent the program from compiling. Find and fix all logical errors which cause the program to crash and/or produce unexpected results. In addition to making sure the code compiles, we have one final method which we need to complete. There is a method in the code, printAll, which is responsible for printing out the entirety...

  • HELLO EXPERTS, I need a code for this 4 questions.... it's make me crazy now... could...

    HELLO EXPERTS, I need a code for this 4 questions.... it's make me crazy now... could you guys help me please? it's java Given two strings, word and a separator sep, return a big string made of count occurrences of the word, separated by the separator string. repeatSeparator("Word", "X", 3)- "WordXWordXWord" repeatSeparator("This", "And", 2)- "ThisAndThis" repeatSeparator("This", "And", 1)- "This"| For example: Result Test System.out.println(repeatSeparator("Pa", "Tn", 4)); PaTnPaTnPaTnPa Consider the series of numbers beginning at start and running up to but...

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