Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

!Please answer all questions please ! 1. Write an expression that executes the loop while the user enters a number greater than or equal to

!Please answer all questions please !

1. Write an expression that executes the loop while the user enters a number greater than or equal to 0.

import java.util.Scanner;

public class NonNegativeLooper { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int userNum;

userNum = scnr.nextInt();

while (/* Your solution goes here */) { System.out.println("Body"); userNum = scnr.nextInt(); } System.out.println("Done."); } }

2.Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow each number by a space. Example output for userNum = 40:

20 10 5 2 1

import java.util.Scanner;

public class DivideByTwoLoop { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int userNum;

userNum = scnr.nextInt();

/* Your solution goes here */

System.out.println(""); } }

3.Write an expression that continues to bid until the user enters 'n'.

import java.util.Scanner;

public class AutoBidder { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); char keepGoing; int nextBid;

nextBid = 0; keepGoing = 'y';

while (/* Your solution goes here */) { nextBid = nextBid + 3; System.out.println("I'll bid $" + nextBid + "!"); System.out.print("Continue bidding? (y/n) "); keepGoing = scnr.next().charAt(0); } System.out.println(""); } }

4. Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints:

1 2 3 4

import java.util.Scanner;

public class ForLoops { public static void main (String [] args) { int userNum; int i;

Scanner input = new Scanner(System.in); userNum = input.nextInt();

for ((i = 1; i <= userNum; i++) {) { System.out.print(i + " "); }

5. Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. Ex: userNum = 3 prints:

0 1 2 3

import java.util.Scanner; public class NestedLoop { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int userNum; int i; int j;

userNum = scnr.nextInt(); } }

6.Given numRows and numColumns, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat, including after the last. Use separate print statements to print the row and column. Ex: numRows = 2 and numColumns = 3 prints:

1A 1B 1C 2A 2B 2C 

import java.util.Scanner; public class NestedLoops { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int numRows; int numColumns; int currentRow; int currentColumn; char currentColumnLetter;

numRows = scnr.nextInt(); numColumns = scnr.nextInt();

/* Your solution goes here */

System.out.println(""); } }

7.Print either "Fruit" or "Drink" (followed by a newline) depending on the value of userItem. For example, if userItem is GR_APPLES, output should be:

Fruit

import java.util.Scanner;

public class GrocerySorter { public enum GroceryItem {GR_APPLES, GR_BANANAS, GR_JUICE, GR_WATER};

public static void main (String [] args) { GroceryItem userItem;

userItem = GroceryItem.GR_APPLES;

/* Your solution goes here */

} }

 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 9 For Linux UNIX And Windows Advanced Database Administration Certification Certification Study Guide

Authors: Roger E. Sanders, Dwaine R Snow

1st Edition

1583470808, 978-1583470800

More Books

Students also viewed these Databases questions