Question

In: Computer Science

Programming Assignment No. 5 Write a program that asks the user to enter a positive odd...

Programming Assignment No. 5
Write a program that asks the user to enter a positive odd number less than 20. If the number is 5, 11 or 15, draw a square whose width is the same as the number entered, if 3, 9, or 17, draw a box (a box is a hollowed out square) with the width the same as the number entered, otherwise just present an message saying "To be determined". Write a method for the square that accepts an integer as a parameter and returns a string (use for loops), the same for the box( use while loops). No output should be done in those methods. Write a method that will ask for the number and does not return until the number entered satisfies the conditions above. Use a do while loop in that method. The method main cannot be used as one of the methods to be written. Use a switch statement to determine which shape to draw. Use the methods from the IO class used in Program 4 for input and output. Modify showMessage so that it uses a fixed-width font otherwise IO should be the same as for Program 4. The main program should be in the default package. Good program structure, adherarnce to Java conventions, and indentation are required. There will be 2 files for this assignment Program5.java which is the default package and IO.java which is in the util package. IO.java will be the same as it was in Program 4. Do not write any more methods than the ones specified. The program above is just a starting point, there are a number of changes that need to be made before. If you are not sure how to get started, a more detailed starting point is in the video.

Solutions

Expert Solution

Problem5.java

import java.util.Scanner;
class Problem5
{
   public static void main(String[] args)
   {  
       // Get input from user
       int n = getInput();
       switch(n)
       {
           // Square case
           case 5:
           case 11:
           case 19:
               System.out.println(square(n));
               break;
           // Box square
           case 3:
           case 9:
           case 17:
               System.out.println(box(n));
               break;
           // Final case
           default:
               System.out.println("To be determined");  
       }
   }

   public static int getInput()
   {
       // Create object for Scanner class to get input from user
       Scanner input = new Scanner(System.in);
       int n;
       do
       {
           // Promt user for input
           System.out.print("Enter odd number less than 20: ");
           // Get input from user
           n = input.nextInt();
       }while(n>20 || n%2==0);//Iterate until given condition satisified
       // Return when n satisified given condition
       return n;
   }

   public static String square(int n)
   {
       // Initialize String square with empty string
       String square = "";
       for(int i=0; i<n; i++)
       {
           for(int j=0; j<n; j++)
           {
               // Add * to square for each iteration
               square += "*";
           }
           // Add a new line to square after each row
           square += "\n";
       }
       // Return square
       return square;
   }
  
   public static String box(int n)
   {
       // Initialize String box with empty string
       String box = "";
       // Initialize i to 0
       int i=0;
       while(i<n)
       {
           // Row starts with *
           box += "*";
           int j=1;
           while(j<n-1)
           {
               // Add * if row it is first or last row, add space otherwise
               box += (i==0||i==n-1)?"*":" ";
               // Increment j by 1
               j++;
           }
           // Row ends with *
           box += "*\n";
           // Increment i by 1
           i++;
       }
       return box;
   }
}

output screenshot:


Related Solutions

Write a python program which asks the user to enter a positive number that is greater...
Write a python program which asks the user to enter a positive number that is greater than 30 called, “num2” and then does the following: o 1) Print all numbers between 1 and “num2” that are divisible by 2 and 3. o 2) Print all numbers between 1 and “num2” that are either divisible by 6 or 7. o 3) Print all numbers between 1 and “num3” that is not divisible by 5
write a program in c++ that asks the user to enter their 5 test scores and...
write a program in c++ that asks the user to enter their 5 test scores and calculates the most appropriate mean. Have the results print to a text file and expected results to print to screen.
Write a java program that asks user to enter a set of positive integer values. When...
Write a java program that asks user to enter a set of positive integer values. When the user stops (think of sentinel value to stop), the program display the maximum value entered by the user. Your program should recognize if no value is entered without using counter.
Please write in Python code please: Write a program that asks the user to enter 5...
Please write in Python code please: Write a program that asks the user to enter 5 test scores between 0 and 100. The program should display a letter grade for each score and the average test score. You will need to write the following functions, including main: calc_average – The function should accept a list of 5 test scores as an input argument, and return the average of the scores determine_grade – The function should accept a test score as...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
In Python write a program that asks the user to enter the monthly costs for the...
In Python write a program that asks the user to enter the monthly costs for the following expenses incurred from operating his or her automobile: loan payment, insurance, gas, oil, tires, and maintenance the program should then display the total monthly cost of these expenses, and the total annual cost of these expenses. your program MUST have BOTH a main function AND a function named calcExpenses to calculate the expenses. DO NOT display the expenses inside of the calcExpenses function!!...
Write a program that asks the user to enter an unsigned number and read it. Then...
Write a program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6. COMMENT COMPLETE CODE PLEASE
In a file called LengthSum.java, write a program that: - Asks the user to enter a...
In a file called LengthSum.java, write a program that: - Asks the user to enter a string. - Asks the user to enter a second string. - Prints out the length of the first string, the length of the second string, and the sum of the two lengths, using EXACTLY the same format as shown below. For example: if the user enters strings "UT" and "Arlington", your program output should look EXACTLY like this: Please enter a string: UT Please...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT