Question

In: Computer Science

Question 1: Write a program to receive a string from the user and compares the first...

Question 1:

Write a program to receive a string from the user and compares the first and second half of the word and prints “First and Second Half Same” or “First and Second Half Different”. If the length of the word is odd, ignore the middle letter.

For example:

Run 1:

Enter a word: abcdabc

First and Second Half Same

Run 2:

Enter a word: abcde

First and Second Half Different

Hint: use the Math.floor() and length() to get the halves of the string and use the equals() to perform the similarity check inside if-else statement.

**********************************************************************************

Question 2:

Write a program that reads three different integers from the users and prints “all the same” if they are all equal, “all different”, if three numbers are different and “neither” for all other cases.

For example:

Enter first number: 3

Enter first number: 2

Enter first number: 6

All Different.

**********************************************************************************

Question 3:

Write a program that reads three different floating point numbers from the users and prints the largest of those three:

For example:

First number: 2.36

Second number: 2.99

Third number: 2.78

Largest: 2.99

Solutions

Expert Solution

ANSWER 1:-


import java.util.*;
public class Main
{
   public static void main(String[] args) {
   Scanner input=new Scanner(System.in);
   System.out.print("Enter a word:");
   String str=input.nextLine();
   int n=str.length();
   int mid=n/2;
   String half=str.substring(0,mid);
   String sub;
  
   if (str.length() % 2==0)
   sub=str.substring(mid);
   else
   sub=str.substring(mid+1);
   if (sub.equals(half))
   System.out.println("First and Second Half Same");
   else
   System.out.println("First and Second Half Different");
  
   }
}

OUTPUT:-

ANSWER 2:-

import java.util.*;
public class Main
{
   public static void main(String[] args) {
   Scanner input=new Scanner(System.in);
   System.out.print("Enter first number:");
   int num1=input.nextInt();
   System.out.print("Enter second number:");
   int num2=input.nextInt();
   System.out.print("Enter third number:");
   int num3=input.nextInt();
  
   if (num1 == num2 && num2 == num3)
   System.out.println("all the same");
   else if (num1 != num2 && num2!=num3)
   System.out.println("all different");
   else
   System.out.println("neither");
   }
}

OUTPUT

Answer 3:-

import java.util.*;
public class Main
{
   public static void main(String[] args) {
   Scanner input=new Scanner(System.in);
   System.out.print("First number:");
   float num1=input.nextFloat();
   System.out.print("Second number:");
   float num2=input.nextFloat();
   System.out.print("Third number:");
   float num3=input.nextFloat();
  
   if (num1>num2){
   if (num1>num3)
   System.out.println("Largest:"+num1);
   else
   System.out.println("Largest:"+num3);
   }
   else{
   if (num2>num3)
   System.out.println("Largest:"+num2);
   else
   System.out.println("Largest:"+num3);
   }
   }
}

OUTPUT:-


Related Solutions

Write a MIPS assembly program that prompts the user first for a string, then for a...
Write a MIPS assembly program that prompts the user first for a string, then for a character. The program should then search the string for the character and print out the number of times the character appears in the string. You can use as many restrictions as you want, just be sure to list them. You must allocate space in the .data segment of your program for the user string.
Question 1: 5pts Write a program to receive two integers as user input and then print:...
Question 1: 5pts Write a program to receive two integers as user input and then print: Sum Difference Product Average Maximum of the two Minimum of the two You may use the min and max functions declared in the math class. ********************************************************************************** Question 2: 10pts An online bank wants you to create a program that will show a prospective customer, how the deposit will grow. Your program should read the initial balance and the annual interest rate. Interest rate is...
Question 1: Write a program to receive two integers as user input and then print: Sum...
Question 1: Write a program to receive two integers as user input and then print: Sum Difference Product Average Maximum of the two Minimum of the two You may use the min and max functions declared in the math class. ********************************************************************************** Question 2: An online bank wants you to create a program that will show a prospective customer, how the deposit will grow. Your program should read the initial balance and the annual interest rate. Interest rate is compounded monthly....
2. Write a Java program to read a string (a password)from the user and then   check...
2. Write a Java program to read a string (a password)from the user and then   check that the password conforms to the corporate password policy.   The policy is:   1) the password must be at least 8 characters   2) the password must contain at least two upper case letters   3) the password must contain at least one digit   4) the password cannot begin with a digit   Use a for loop to step through the string.   Output “Password OK” if the password...
Write a program that: a.Asks the user for their first name.b.Asks the user for their...
Write a program that: a.Asks the user for their first name. b.Asks the user for their age. c.Asks the user for their last name. d.Print out to the user the following information: i.Number of characters in the first & last name combined ii.The full name all in upper case letters iii.The full name all in lower case letters iv.The first letter of the name v.The age Compile and test your code in NetBeans and then on Hackerrank at www.hackerrank.com/csc127-chapter2-classwork then...
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
Creating a list/tuple 4. Write a program to accept a string from the user, then replace...
Creating a list/tuple 4. Write a program to accept a string from the user, then replace the first letter with the last letter, the last letter with the middle letter and the middle letter with the first letter. E.g. “Billy” -> “yiBll”.
Write a program that prompts the user for their first and lastname. Display the first...
Write a program that prompts the user for their first and last name. Display the first initial of their first name and their last name to the user.Ask the user to input a phone number.The program checks which part of Colorado a phone number is from using the values below.If the second digit of the phone number is one of the below digits, print the phone number and which part of Colorado it is from. If none of the digits...
Question 2. Write a MARIE program that accepts an integer from the user, and if it...
Question 2. Write a MARIE program that accepts an integer from the user, and if it is a prime number the program will output 1, otherwise, the program will output 0. Examples: If the user input is 17, the output would be 1 If the user input is 2, the output would be 1 If the user input is 15, the output would be 0 If the user input is -2, the output would be 0 You should write and...
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT