Question

Here is my code to put a quote down, flip it, and reverse it. I do...

Here is my code to put a quote down, flip it, and reverse it. I do not have much knowledge on coding. Does my code only use Variables, Console I/O, Java Program Design, Intro to IDEs, If Statements, Selection and Conditional Logic, Strings, Loops, Nesting, and Iteration? If not, could it be fixed to only use them?

These are the offical steps of the assignment:

- Ask a user to enter a quote - whether it's several sentences or a single word, we should be able to read in and store that quote.
- Create a loop that prints every character of the string with a space between it.
EG: "Hey there!" should print "H e y t h e r e ! "
- Create a loop that examines every letter of the quote, and results in printing it out backward. Think about loop design, and what the most efficient way to design "backward text" would be.
EG: "Hello world" should print "dlrow olleH"
- Once you have the text printing backward, modify the code so the reversed text is stored in a "backward text" string. Print the stored string instead of simply using print statements in a loop on the original string.
KEEP BOTH VERSIONS IN YOUR CODE - you should be printing the text backward twice now - once without a variable, and once with just a variable printing that holds the backward text.
- Use a loop to keep track of how many of each of the five main vowels (a, e, i, o, u) are in the original string text in total, and print a statement each time you find a vowel - you should be going in order. Once you have read the original text left to right, you should print the total vowels
EG: "apples are tasty yup" has 3 a's and 2 e's and 1 u, should print a statement pattern similar to:
a found
e found
...
u found
Text has the following vowel counts: a = 3, e = 2, i = 0, o = 0, u = 1
- After you have your vowel counts, divide them by the length of the quote to retrieve the average of each vowel in the quote.
EG: Text has the following vowel average: a = 15%, e = 10%, i = 0%, o = 0%, u = 5%
- Put all of the above work you've done into a loop that will allow you to repeatedly enter quotes. After the first time of entering quotes and doing all of the actions above, ask the user if they would like to enter another quote. If a user enters 1, allow the loop to repeat. Terminate the loop if the user enters 0, to stop typing quotes. Inform the user of the action expected with a print statement, such as: Please enter 1 to enter another quote, or 0 to exit.

Here is my code:

import java.util.*;
class Quote
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
//n is the number of test case u can have enter any number of string
int n=sc.nextInt();
sc.nextLine();
for(int i=0;i<n;i++)
{
System.out.println("Enter the string");

String str=sc.nextLine();
System.out.println("The entered string is "+ str);
//len is the variable that holds the length of the entered strinng
int len=str.length();
System.out.println("The entered string with the space in between the each character");
for(int j=0;j<str.length();j++)
{
System.out.print(str.charAt(j)+" ");
}
System.out.println("the reverse of the given string is");
for(int j=str.length()-1;j>=0;j--)
System.out.print(str.charAt(j));
System.out.println();
for(int j=0;j<str.length()-1;j++)
{
if(str.charAt(j)=='a' || str.charAt(j)=='e'|| str.charAt(j)=='i'||str.charAt(j)=='o'||str.charAt(j)=='u')
{
System.out.println(str.charAt(j)+" is found");
}
}
int counta=0,counte=0,counti=0,countu=0,counto=0;
for(int j=0;j<str.length();j++)
{
if(str.charAt(j)=='a')counta++;
else if(str.charAt(j)=='e')counte++;
else if(str.charAt(j)=='i')counti++;
else if(str.charAt(j)=='o')counto++;
else if(str.charAt(j)=='u')countu++;
}
System.out.println("The count of a is "+counta);
System.out.println("The count of e is "+counte);
System.out.println("The count of i is "+counti);
System.out.println("The count of o is "+counto);
System.out.println("The count of u is "+countu);

  
  
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

public class Quote{

public static void main(String args[]){

Scanner sc= new Scanner(System.in); //scanner for user input

int con=0; //variable to allow user to enter another quote

do {

System.out.println("Enter the Quote: ");

String quote; //variable to store quote

quote=sc.nextLine(); //store user quote

int len=quote.length(); //store length of quote

String rev=""; //store reversed string

int counta=0,counte=0,counti=0,countu=0,counto=0; //declare & initialize variables to store count of vowels in quote

System.out.println("The entered quote with the space in between the each character : ");

for(int j=0;j<len;j++){

if(quote.charAt(j)!=' ')

System.out.print(quote.charAt(j)+" "); //avoid multiple spaces between words if not required remove IF condition

//count the vowels " a, e, i, o, u" in quote

if(quote.charAt(j)=='a')counta++;

else if(quote.charAt(j)=='e')counte++;

else if(quote.charAt(j)=='i')counti++;

else if(quote.charAt(j)=='o')counto++;

else if(quote.charAt(j)=='u')countu++;

}

System.out.println();

System.out.println("Reverse of the given quote as each character : ");

for(int j=len-1; j>-1; j--) { //print string in reverse

System.out.print(quote.charAt(j));

rev+=quote.charAt(j); //storing reversed string

}

System.out.println();

System.out.println("Reverse of the given quote stored in single variable : ");

System.out.println(rev);

System.out.println();

//printing vowels found in given quote

System.out.println("Vowels in the given Quote : ");

for(int j=0; j<len; j++){

if(quote.charAt(j)=='a' || quote.charAt(j)=='e'|| quote.charAt(j)=='i'|| quote.charAt(j)=='o'|| quote.charAt(j)=='u'){

System.out.println(quote.charAt(j)+" is found");

}

}

//Number of each vowel found in the given quote

System.out.println();

System.out.println("The count of 'a' is : "+counta);

System.out.println("The count of 'e' is : "+counte);

System.out.println("The count of 'i' is : "+counti);

System.out.println("The count of 'o' is : "+counto);

System.out.println("The count of 'u' is : "+countu);

System.out.println();

//declare & initialize variables for average of vowels in the quote

int aavr=0, eavr=0, iavr=0, oavr=0, avr=0;

//printing average of vowels in the quote

System.out.println("Average of vowel 'a' is : "+counta*100/len+"%");

System.out.println("Average of vowel 'e' is : "+counte*100/len+"%");

System.out.println("Average of vowel 'i' is : "+counti*100/len+"%");

System.out.println("Average of vowel 'o' is : "+counto*100/len+"%");

System.out.println("Average of vowel 'u' is : "+countu*100/len+"%");

System.out.println();

System.out.print("Please enter 1 to enter another quote, or 0 to exit : ");

con=sc.nextInt();

System.out.println(sc.nextLine()); //to remove buffer stored in scanner

}while(con!=0);

System.out.println("User not allowed to enter any more Quotes.");

}

}

Add a comment
Know the answer?
Add Answer to:
Here is my code to put a quote down, flip it, and reverse it. I do...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Take the following C++ code and add to it the following: Write a program that reads...

    Take the following C++ code and add to it the following: Write a program that reads a string and outputs the number of times each lowercase vowel appears in it. Your program must contain a function with one of its parameters as a char variable, and if the character is a vowel, it increments that vowel's count. #include<iostream> #include<string> using namespace std; int countVowel(char, char); int main(void) { string str1; int countA = 0; int countE = 0; int countI...

  • Hello, I have a assignment where the user inputs a word and the out will then...

    Hello, I have a assignment where the user inputs a word and the out will then tell the user how many of the five vowels are in the word so for example if I typed the word hello, it would output {0 1 0 0 0 } since there is only one vowel and that is e which is in the second row, I have most of it done, but I can't seem to figure out how to get the...

  • Hello! I have a Java homework question that I could really use some help with. I...

    Hello! I have a Java homework question that I could really use some help with. I keep getting an error, but I don't know what is wrong with my code. Thanks so much in advance! The problem is: 6.9: VowelAnalyst Design and implement an application that reads a string from the user, then determines and prints how many of each lowercase vowel (a, e, i, o, and u) appear in the entire string. Have a separate counter for each vowel....

  • Hi im getting a Exception in thread "main" java.lang.NullPointerException. What is wrong? Here is my code....

    Hi im getting a Exception in thread "main" java.lang.NullPointerException. What is wrong? Here is my code. class MicrosoftMonarchs { //declaring all the arrays public String array[]; public double close[]; public int volume[]; public double open[]; public double high[]; public double low[]; //declaring both size and totalFileData public long totalFileData=0; public int size=5; //main file public static void main(String[] args) { MicrosoftMonarchs data = new MicrosoftMonarchs(); //setting all the arrays equal to the size of the given text data.array= new String[data.size];...

  • Only part 1, makinf the 2nd and 3rd picture of code do the same thing using...

    Only part 1, makinf the 2nd and 3rd picture of code do the same thing using array Purpose This homework is to learn the concept and usage of one-dimensional (ID) arrays. Part 1 (30 pts) Modify your HW6' (i.e., digit frequency histogram) in a new program (DigitFrequencyArray.java) to use a count array instead of 10 count variables. Your new program should be much shorter than your original code since there is no need for branching statements (if or switch). For...

  • In this same program I need to create a new method called “int findItem(String[] shoppingList, String...

    In this same program I need to create a new method called “int findItem(String[] shoppingList, String item)” that takes an array of strings that is a shopping list and a string for an item name and searches through the “shoppingList” array for find if the “item” exists. If it does exist print a confirmation and return the item index. If the item does not exist in the array print a failure message and return -1. import java.util.Scanner; public class ShoppingList...

  • please edit my java code perferably on jgrasp version 50.0 , wont complie correctly :( the...

    please edit my java code perferably on jgrasp version 50.0 , wont complie correctly :( the program is supposed to read from my input file and do the following        1) print out the original data in the file without doing anything to it.        2) an ordered list of all students names (last names) alphabetically and the average GPA of all student togther . 3) freshman list in alphabeticalorder by last name with the average GPA of the freshmen...

  • Hello! I have a problem in my code please I need help, I don't know How I can wright precondition, so I need help about assertion of pre_condition of peek. Java OOP Task is! Improve the circular a...

    Hello! I have a problem in my code please I need help, I don't know How I can wright precondition, so I need help about assertion of pre_condition of peek. Java OOP Task is! Improve the circular array implementation of the bounded queue by growing the elements array when the queue is full. Add assertions to check all preconditions of the methods of the bounded queue implementation. My code is! public class MessageQueue{ public MessageQueue(int capacity){ elements = new Message[capacity];...

  • Here is everything I have on my codes, the compiler does not allow me to input...

    Here is everything I have on my codes, the compiler does not allow me to input my name, it will print out "Please enter your name: " but totally ignores it and does not allow me to input my name and just proceeds to my result of ID and Sale. Please help. I've bolded the part where it I should be able to input my name package salesperson; public class Salesperson { String Names; int ID; double Sales; // craeting...

  • Having trouble with the do while/while loop and the switch statement. I got some of the...

    Having trouble with the do while/while loop and the switch statement. I got some of the switch statement but cant get the program to repeat itself like it should.What i have so far for my code is below. Any help is appreciated... i am not sure what I am doing wrong or what i am missing. I am completely lost on the while loop and where and how to use it in this scenario. import java.util.Scanner; public class sampleforchegg {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT