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 this C++ program using classes 1. Create a file text file with a string on...
Create this C++ program using classes 1. Create a file text file with a string on it 2. Check the frecuency of every letter, number and symbol (including caps) 3. Use heapsort to sort the frecuencys found 4. Use huffman code on the letters, symbols or numbers that have frecuencys I created the file, and the frecuency part but i'm having trouble with the huffman and heapsort implementation.
Write a program in java which is in main and uses no classes, methods, loops or...
Write a program in java which is in main and uses no classes, methods, loops or if statements Ask the user to enter their first name Ask the user to enter their last name Print their initials followed by periods (ie. Clark Kent = C. K.) Print the number of letters in their last name
3) Create a Java program that uses NO methods, but use scanner: Write a program where...
3) Create a Java program that uses NO methods, but use scanner: Write a program where you will enter the flying distance from one continent to another, you will take the plane in one country, then you will enter miles per gallon and price of gallon and in the end it will calculate how much gas was spend for that distance in miles. Steps: 1) Prompt user to enter the name of country that you are 2) Declare variable to...
You do not need to import the Math class into your Java program to use methods...
You do not need to import the Math class into your Java program to use methods or constants in it. Group of answer choices True False
In Python, create a program with 2 classes that do the following. HashCreate class, this class...
In Python, create a program with 2 classes that do the following. HashCreate class, this class will accept a directory and hash each file in the directory and store the results in a dictionary. The dictionary will contain the hash value and file name. HashVerify, the second class will accept the dictionary as input and save that in an instance attribute. This class must also contain a method for lookups that require a file path as input. The lookup method...
rite a java program that uses the methods written in this section to do the following:Prompt...
rite a java program that uses the methods written in this section to do the following:Prompt the user for a number of grades using the getValidInput method. Ensure the number of grades is greater than 1 and less than 30. The error message is “You must enter a number between 1 and 30, please re-enter:”Prompt the user to enter the requested grades and total marks using the calculateLetterGrade method. The grades should be between 0 and 100. Display an appropriate...
Create and initialize a string array, write and use 3 functions.Write a program. Create a string...
Create and initialize a string array, write and use 3 functions.Write a program. Create a string array in the main(),called firstNameArray, initialize with7 first namesJim, Tuyet, Ann, Roberto, Crystal, Valla, Mathilda Write a first function, named searchArray, that passes two arguments: a single person’s name and the array reference, into a function,This function should then search the array to see if the name is in the firstNameArray. If found, it will return the array index where the name is found,...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
Write a program that prompts the user to input a string. The program then uses the...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning of your program and...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT