Question

In: Computer Science

Please create using only For loops, as simple as possible as I am attending Java1 Create...

Please create using only For loops, as simple as possible as I am attending Java1

Create two Java programs that do the following:

a. Use a for loop to print the numbers below on a single line as shown.

1 2 4 8 16 32 64 128.

b. Ask the user to enter numbers using the for loop.  First ask the user how many numbers they will be entering in. Print the sum and average of the entered numbers.

c. Write a program that is a salute to pets. Ask the user three questions:
* What is the name of your favorite pet?
* How many times would you like to see the name appear on the screen?
* Would you like to see the names one per line, or arranged horizontally?

Using a for loop, write a program to accomplish task the task above. Put in an error trap to guarantee that the user does not enter a negative number for the number of times the name is to appear. You could assume that the numbers are going to be integers.

Solutions

Expert Solution

For part a and b.

Code:

Output:

For part c:

Code:

import java.io.*; // imports all class from java.io package
import java.util.Scanner; //scanner class found in java.util package
public class Main
{
   public static void main(String[] args) {
  
   int d,i; // variable declaration
   String name,f; //variable declaration
   Scanner sc1= new Scanner(System.in);//Declaring sc1 as an object of scanner class
   Scanner sc2= new Scanner(System.in);//Declaring sc2 as an object of scanner class
       System.out.println("What is the name of your favorite pet?");
       name=sc1.nextLine();//input
       System.out.println("How many times would you like to see the name appear on the screen?");
       d=sc1.nextInt();//input
       System.out.println("Would you like to see the names one per line, or arranged horizontally?");
       f=sc2.nextLine();//input
   if(d>0)
   {
       if(f.equalsIgnoreCase("one per line")) //compares two strings irrespective of case
       {
       for(i=0;i<d;i++)
       {
       System.out.println(name); // display
       }
       }
       else if(f.equalsIgnoreCase("horizontally")) //compares two strings irrespective of case
       {
       for(i=0;i<d;i++)
       {
       System.out.print(name+"\t"); // display
       }
       }
       else{
       System.out.println("Enter a valid input"); // for invalid input
       }
   }
   else{
  
   System.out.print("Enter a valid input"); // for invalid input
   }
   }
  
}
Output:


Related Solutions

(I AM IN A INTRO TO PROGRAMMING, PLEASE USE SIMPLE PSEUDOCODE IF POSSIBLE) Create pseudocode for...
(I AM IN A INTRO TO PROGRAMMING, PLEASE USE SIMPLE PSEUDOCODE IF POSSIBLE) Create pseudocode for a program for Hansel's Housecleaning Service. The program prompts the user for a customer's last name only. While the last name is not “zzz” your program will ask for the number of bathrooms and the number of other rooms to be cleaned and display the cleaning charge. You should use a sentinel-controlled while loop in your main loop logic. A customer name of “zzz”...
I am using a photoshop character that I drew. Please provide AS SIMPLE OF A PROCEDURE...
I am using a photoshop character that I drew. Please provide AS SIMPLE OF A PROCEDURE AS POSSIBLE!! 1. How do you create a sprite in Unity? 2. How do you create a sprite sliced in Unity? Thank you :)
In the given instruction, I am to use while loops only. The goal is to prompt...
In the given instruction, I am to use while loops only. The goal is to prompt the user to select an acceptable input. If the wrong input is given, the program forces the user to select an appropriate input. The program also keeps running until the user chooses to exist by selecting a very specific input which in my case is the upper or lower case "E". The problem is even after selecting upper or lower case "E", my program...
Please write in C++ as simple as possible I want you to create a Book Class...
Please write in C++ as simple as possible I want you to create a Book Class for a bookstore. I am not going to tell you what variables should go into it, that is for you to figure out (but you should have at least 5+). And then you must create a UML with all the variables and methods (like: the getters and setters, a default constructor, and a constructor that takes all the variables and finally the printValues() )....
I am using a drawing done in my photoshop. Please provide a simple procedure for how...
I am using a drawing done in my photoshop. Please provide a simple procedure for how to do these tasks in UNITY. 1. How do you do an animation clip screenshot? 2. How do you show or do the animator screenshot layer?
Please write the SQL statement for the following. I am using the Adventurework2014 database. 1. Create...
Please write the SQL statement for the following. I am using the Adventurework2014 database. 1. Create a login for AdventureWorks employees. An employee login should be composed of the first letter of a person's first name combined with their last name. In addition, the login should be all lower case characters. All the required information is located in Person.Person table. Employees can be identified by "EM" value in the PersonType field. The output should include BusinessEntityID, first name (FirstName), last...
I am in beginners java course so using the most simple/basic JAVA please complete the following...
I am in beginners java course so using the most simple/basic JAVA please complete the following CODE SEGMENTS. (2 parts) FIRST PART: Using ITERATIVE create a METHOD MAX that returns the max element in an ArrayList of ints and prints all of the elements. *you have to use an iterative in the method.* (just need to write the METHOD ONLY not a whole program(will do in 2nd part), assume you are given any numbers/integers. SECOND PART: Now write a class...
PLEASE EXPLAIN ANSWER. USING JAVA via jGRASP i am getting an error messge a. Create a...
PLEASE EXPLAIN ANSWER. USING JAVA via jGRASP i am getting an error messge a. Create a class named Sandwich. Data fields include a String for the main ingredient (such as tuna), a String for bread type (such as wheat), and a double for price (such as 4.99). Include methods to get and set values for each of these fields. Save the class as Sandwich.java. b. Create an application named TestSandwich that instantiates one Sandwich object and demonstrates the use of...
Please complete in only C++, using loops Assignment: For this assignment you’ll be designing a program...
Please complete in only C++, using loops Assignment: For this assignment you’ll be designing a program which can take the input of a decimal number and a numerical base, and convert the decimal number to that base. For example, if given the decimal number seven and the base two, your program should output it as 111, which is how seven is represented in binary. Another example, 8,943 in base 10, is 13,236 in base 9. You’ll need to perform these...
Using PHP and MYSQL and with a simple customer database, how can I create a simple...
Using PHP and MYSQL and with a simple customer database, how can I create a simple log in and registration system for an ecommerce site
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT