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

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...
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 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...
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...
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...
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:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT