Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1: Written Exercises (4 points) Note: The answers to the following question

ID: 3791681 • Letter: P

Question

Part 1: Written Exercises (4 points)

Note: The answers to the following questions should be typed in the block of comments in the Assignemnt3.java file.  Please make sure they're commented out. Type them neatly and make them easy to read for the graders.

Exercise1: What output is produced by the following code fragment? (1 pt)

String m1, m2, m3;

m1 = "Java Programming";

m2 = m1.toLowerCase( );

m3 = m1 + " " + m2;

System.out.println(m3.replace('a','o'));

Exercise2: Write an assignment statement that computes the square root of the sum of num1 and num2 and assigns the result to num3. (2 pts)

Exercise3: What output is produced by the following code fragment? (1 pt)

int num = 57, max = 35;

if (num >= max*2)

System.out.println("orange");

System.out.println("lemon");

System.out.println("apple");

Part 2: Programming (16 points)

Write a complete Java program in a source file to be named Assignment3.java.

The program should ask a user to enter two strings.
First, the program prompts:

Please enter a string.

The program should read in the string a user enters, and prompts:

Please enter another string.

The program reads in the string and performs the following:

checks if each string's length is odd or even.

checks if two strings are same or different.

checks which string is lexically larger, or they are same.

prints out the first character (index 0) of each string

prints out a new string consisting of the first string concatenated (appended) with the second string.

prints out two strings using upper case letters.

Here is the output your program should produce when the user enters the string shown in bold:

Sample Output: (the user enters the string shown in bold)

Please enter a string.
Apple
Please enter another string.
Orange

The first string's length is odd.
The second string's length is even.

The two strings are different.

The second string is lexically larger.

The first character of the first string is A
The first character of the second string is O

The concatenation of two strings is "AppleOrange"

The first string using upper case letters: APPLE
The second string using upper case letters: ORANGE

Explanation / Answer

Hi,

Please see below the java classes. Answers to Part 1 questions are added in Assignment.javaand these answers are commented. Please comment for any queries/feedbacks.

Thanks,

Anita

Assignment3.java

import java.util.Scanner;

//------------------------------------
//Part 2: Programming
//------------------------------------
public class Assignment3 {

   public static void main(String [] args){
       String firstString = "";
       String secondString = "";
       int length =0;
       Scanner scan = new Scanner(System.in);
      
       //Prompting the user for First String
       System.out.println("Please enter a String : ");
       firstString = scan.nextLine();
      
       //Prompting the user for Second String
       System.out.println("Please enter another string :");
       secondString = scan.nextLine();
       System.out.println();
      
       //checking 1st string's length is even or odd
       length = firstString.length();
       if(length % 2 != 0){
           System.out.println("The first string's length is odd.");
       }
       else{
           System.out.println("The first string's length is even.");
       }
       System.out.println();
      
       //checking 2nd string's length is even or odd
       length = secondString.length();
       if(length % 2 != 0){
           System.out.println("The second string's length is odd.");
       }
       else{
           System.out.println("The second string's length is even.");
       }
       System.out.println();
      
       //Checking if 2 strings are saemor different
       if(firstString.equalsIgnoreCase(secondString)){
           System.out.println("The two strings are same.");
       }
       else{
           System.out.println("The two strings are different.");
       }
       System.out.println();
      
       //Checking which string is lexically larger, or they are same.
       int compareVal = firstString.compareTo(secondString);
       if(compareVal > 0){
           System.out.println("The first string is lexically larger.");
       }
       else if(compareVal < 0){
           System.out.println("The second string is lexically larger.");
       }
       else if(compareVal == 0){
           System.out.println("two strings are lexically equal.   ");
       }
       System.out.println();
      
       //prints out the first character (index 0) of each string
       System.out.println("The first character of the first string is "+firstString.charAt(0));
       System.out.println("The first character of the second string is "+secondString.charAt(0));
       System.out.println();
      
       //To print a new string consisting of the first string concatenated (appended) with the second string.
       String newStr = firstString + secondString;
       System.out.println("The concatenation of two strings is "+newStr);
       System.out.println();
      
       //to print out two strings using upper case letters.
       System.out.println("The first string using upper case letters: "+firstString.toUpperCase());
       System.out.println("The second string using upper case letters: "+secondString.toUpperCase());
       System.out.println();
   }

}

//------------------------------------------
//       PART -1
//-------------------------------------------
//Please see the answers of Part-1 questions below.

//--------Exercise-1 ---------------------
/*


Exercise1: What output is produced by the following code fragment? (1 pt)

String m1, m2, m3;
m1 = "Java Programming";
m2 = m1.toLowerCase( );
m3 = m1 + " " + m2;
System.out.println(m3.replace('a','o'));


Answer:

Jovo Progromming jovo progromming

//--------Exercise-2 ---------------------

Exercise2: Write an assignment statement that computes the square root of the sum of num1 and num2 and assigns the result to num3. (2 pts)


Answer:

num3 = Math.sqrt(num1+num2);

//--------Exercise-3 ---------------------

Exercise3: What output is produced by the following code fragment? (1 pt)

int num = 57, max = 35;
if (num >= max*2)
System.out.println("orange");
System.out.println("lemon");
System.out.println("apple");

Answer:
lemon
apple

*/

Sample output for Assignment3.java

Please enter a String :
Apple
Please enter another string :
Orange

The first string's length is odd.

The second string's length is even.

The two strings are different.

The second string is lexically larger.

The first character of the first string is A
The first character of the second string is O

The concatenation of two strings is AppleOrange

The first string using upper case letters: APPLE
The second string using upper case letters: ORANGE

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote