Question

In: Computer Science

Objective: Create a program that uses the methods in the String and Math classes to do...

Objective:

Create a program that uses the methods in the String and Math classes to do the following:

  • Ask the user to enter two nouns
  • Tell the user how many letters the nouns are from eachother in the dictionary.
  • Tell the user the last letter of each of the nouns
  • Tell the user the position of the letter "e" in each of the nouns, -1 if no "e" exists in the word. For full credit, you must use quotations around e in the outprint display as shown below.
  • Ask the user to enter three integers for the values of A, B, and C in a quadratic equation.
  • Calculate and print the roots of the corresponding quadratic equation. You should remember the quadratic formula from a math class, if not, look it up!
  • Note: if no roots exist, Nan (Not a number) will display.

Solutions

Expert Solution

import java.io.*;
class Program
{
    public static void main(String[]args)throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String str1, str2;
        char ch;
        int l1, l2, letters, i, flag1=0, flag2=0, a, b, c;
        double d, r1, r2;
        System.out.println("Enter first noun");
        str1=br.readLine();
        System.out.println("Enter second noun");
        str2=br.readLine();
        l1=str1.length();// length of first noun
        l2=str2.length();// length of second noun
        letters=str1.compareToIgnoreCase(str2);// comparing the two nouns lexicographically
        System.out.println("The diference between the position of two nouns in dictionary is "+letters);
        System.out.println("Last letter of "+str1+" is "+str1.charAt(l1-1));// printing last letter of first noun
        System.out.println("Last letter of "+str2+" is "+str2.charAt(l2-1));// printing last letter of second noun
        for(i=0; i<l1; i++)
        {
            ch=str1.charAt(i);
            if(ch=='e'||ch=='E')
            {
                System.out.println("The position of letter "+ "e" +" in "+str1+" is "+(i+1));//printing position of "e" in first noun
                flag1=1;
                break;
            }
        }
        for(i=0; i<l2; i++)
        {
            ch=str2.charAt(i);
            if(ch=='e'||ch=='E')
            {
                System.out.println("The position of letter "+"e"+" in"+str2+" is "+(i+1));//printing position of "e" in second noun
                flag2=1;
                break;
            }
        }
        if(flag1==0)
            System.out.println("The position of letter "+"e"+" in "+str1+" is -1");
        if(flag2==0)
            System.out.println("The position of letter "+"e"+" in "+str2+" is -1");
        System.out.println("Enter three integer for values of a, b and c in a quadratic equation");
        a=Integer.parseInt(br.readLine());
        b=Integer.parseInt(br.readLine());
        c=Integer.parseInt(br.readLine());
        d=(b*b)-4*a*c;// calculating discriminant
        System.out.println("Roots of the quadratic equation  ("+a+")x^2+("+b+")x+("+c+") are:");
        if(d<0) //if discriminant less than zero roots don't exist
            System.out.println("NAN(Not a number");
        else
        { //if discriminat greater than zero roots exist
            r1=((-b)+Math.sqrt(d))/(2*1); // using Math.sqrt function
            r2=((-b)-Math.sqrt(d))/(2*1); // using Math.sqrt function
            System.out.println(r1+" and "+r2);
        }

    }
}


Related Solutions

Create a program that accepts in a string of 2 or more words. The program then...
Create a program that accepts in a string of 2 or more words. The program then copies the entered string changing the alpha characters into digits representing characters with acsenders, descenders and nonascender/desender characters; uppercase characters should be treated as lower case. The characters with descenders (gjpqy) should be replaced with a 1. The characters with ascenders (dbfhklt) should be replaced with a 2. The rest of the alpha characters should be replaced with a 3. The converted string should...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
Create a moderately complex java program that utilises 2 or more classes. Within these classes: -...
Create a moderately complex java program that utilises 2 or more classes. Within these classes: - have one that defines an exception - have that exception throw(n) in one method and handled in another -has the program continue even if the user inputs incorrect data -be creative/unique in some way
In objective-C Task: Write a program whose input is a character and a string, and whose...
In objective-C Task: Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1.You may assume that the string does not contain spaces and will always contain less than 50 characters. Ex: If the input is: n Monday the output is:...
Create program which sorts letters of a string based on ASCII value. The program will then...
Create program which sorts letters of a string based on ASCII value. The program will then print the sorted string to stdout. Use C programming language. - Only use stdio.h - Input prompt should say "Enter string of your choice: " - Remove any newline \n from input string - Implement sorting operation as a function. Should use selection sort algorithm, but you may use a different algorithm - Output should print sorted string on new line Example:     Enter...
** USING MATLAB TO PROGRAM The main objective of this lab is to create a game...
** USING MATLAB TO PROGRAM The main objective of this lab is to create a game that involves betting on the sum of two dice. The player will start out with some initial total amount of money. During each round, the player can bet some money that the sum of the two dice will be equal to a certain number. If the player wins the bet, that player adds the amount of the bet to his or her current total....
java script coding to create a math test program with 5 questions.
java script coding to create a math test program with 5 questions.
Write a program to use Math methods to calculate and display the followings: Prompts the user...
Write a program to use Math methods to calculate and display the followings: Prompts the user to enter an angle, in degrees, and then display the angle in radians and the Sine of the angle. Prompts the user to enter a positive integer and calculates the square root of the number. Computational thinking: Your program must check if it is a positive number. If a negative number is entered, it must ask for another number until the positive number is...
The following program uses Pthreads to create two threads. They do some work for the process...
The following program uses Pthreads to create two threads. They do some work for the process and then exit. The process then outputs a result. Assume all supporting libraries and other functions have been included. => Use the answer text field to describe what work (operations) the threads are doing, and what kind of result (what is it?) is output by the process. #include <pthread.h> #include <stdio.h> #include <stdlib.h> int res1, res2, a[100], b[100]; void *runner1(void *param); void *runner2(void *param);...
Objective: Create a program that has a class to represent a cup of coffee that combines...
Objective: Create a program that has a class to represent a cup of coffee that combines the properties: name and caffeine content. The class should also have a method that calculates the number of cups that would be maximally risky to consume in a short period of time. The program should create two instances of the class coffee where the user enters their properties and should output the amount of coffees that consumed would be risky. Requirements: Write a class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT