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 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 :)
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...
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...
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...
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
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops,...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops, if loops, while loops, arrays and conditional execution, as well as creating an image of the game board. The list below must be considered in the code. - Selecting a word from a dictionary (a text file) - Reading a single letter from the user - Building a character array showing the letters matched so far - Keeping count of the number of guessed...
Hi I am having the following problem. At the moment I am trying to create a...
Hi I am having the following problem. At the moment I am trying to create a bode plot for the following function. G(s)=(Ks+3)/((s+2)(s+3)) Note: Not K(s+2)! I then want to plot multiple bode plots for various values of K. Eg. 1,2,3, etc. I am having two separate issues. 1. How do I define the TF with a constant K in the location required (a multiple of s in the numerator) 2. How do I create multiple bode plots for values...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT