Question

In: Computer Science

Write a program (your choice!) that fulfills all the requirements below. The final product should include...

Write a program (your choice!) that fulfills all the requirements below. The final product should include the pseudocode and flowchart for the entire program.

  • Variables of each data type
    • String, Integer, Real, Boolean
  • Must contain at least one type of calculation
  • Minimum of 3 modules/functions
  • At least 2 decision structures
    • IF, IF-ELSE, IF-ELSE IF
  • At least 1 type of loop
    • WHILE or FOR
  • At least 1 array/list

Solutions

Expert Solution

Explanation:

Program is also provided at the end.The program was developed using eclipse Mars Release (4.5.0).

I have written the program to demonstrate the following concepts:

  • Variables of each data type
    • String, Integer, Real, Boolean
  • Must contain at least one type of calculation
  • Minimum of 3 modules/functions
  • At least 2 decision structures
    • IF, IF-ELSE, IF-ELSE IF
  • At least 1 type of loop
    • WHILE or FOR
  • At least 1 array/list

Program Description:

Program is completely coded in Java

The program requires user to enter their name which is stored in a String variable.Next the user is asked to select the type of operation that needs to be performed.

Choice of operations are represented by integer number 1 being the lowest and 4 being the highest input.

The type of Operations and their corresponding Integer numbers are given below:

1.Print Fibonacci series 2.Calculate Factorial 3.Print Odd Even numbers upto 100 4.Exit

After the user selects the type of operations different functions are called based on the option selected by the user.

Three Functions have been defined in the following way:

public static int factorial(int n){
       int i,fact=1;
       for(i=1;i<=n;i++){
       fact=fact*i;
       }
       return fact;
   }
  
   public static void fibonnaci(int limit){

       long[] series = new long[limit];
      
       //create first 2 series elements
       series[0] = 0;
       series[1] = 1;

       //create the Fibonacci series and store it in an array
       for(int i=2; i < limit; i++){
           series[i] = series[i-1] + series[i-2];
       }
      
       //print the Fibonacci series numbers
      
       System.out.println("Fibonacci Series upto " + limit);
       for(int i=0; i< limit; i++){
           System.out.print(series[i] + " ");
       }
   }
  
   public static void printOddEven(){
       for(Integer i=1;i<=100;i++){
           if(i%2==0){
               System.out.println("Number is even"+i);
           }else{
               System.out.println("Number is odd"+i);
           }
       }
   }

Pseudo code : -

The Pseudo code for the above program consist of the following steps

Step 1 : Take name as an input from the user and store it in as a String variable

Step 2 : Provide the user with the type of operations that can be performed and their corresponding options such as

1.Print Fibonacci series 2.Calculate Factorial 3.Print Odd Even numbers upto 100 4.Exit

Step 3 : Take the input from the user and store it in a variable named nextInt

IF nextInt == 1 then

Ask the user to input the number till which fibonnaci series needs to be generated and store it in a variable named fib such as

fib=sc.next();

Pass the number provided as an input to the function fibonnaci(fib);

IF nextInt==2 then

Ask the user to input the number till whose factorial needs to be calculated and store it in a vairable named fin

Print the fibonnaci series

fin=sc.next();

Pass the number provided as an input to the function factorial(fin);

Print the factorial of the given number

IF nextInt==3 then

Print Odd Even numbers upto 100.

ELSE

Exit the Program.

Step 4: Provide user with the prompt Do you wish to continue.Type y or n

IF user choose N

Exit the Program.

ELSE IF user choose Y

Repeat Steps 2 and 3.

ELSE

Exit the Program.

Flow Chart for the above Program:

Flow Chart depicts the flow of the program.Program Explanation has been given in the beginning and flowchart is self -explanatory

Actual Program: Save the Program by the name OddEven.java

import java.util.Scanner;

public class OddEven {

   private static Scanner sc;

//Function that demonstrates use of For Loop and calculates the factorial

   public static int factorial(int n){
       int i,fact=1;
       for(i=1;i<=n;i++){
       fact=fact*i;
       }
       return fact;
   }
//Function that uses array and also uses Real Number
   public static void fibonnaci(int limit){

       long[] series = new long[limit];
      
       //create first 2 series elements
       series[0] = 0;
       series[1] = 1;
      
       //create the Fibonacci series and store it in an array
       for(int i=2; i < limit; i++){
           series[i] = series[i-1] + series[i-2];
       }
      
       //print the Fibonacci series numbers
  
       System.out.println("Fibonacci Series upto " + limit);
       for(int i=0; i< limit; i++){
           System.out.print(series[i] + " ");
       }
   }
//Function that uses Integer datatype
   public static void printOddEven(){
       for(Integer i=1;i<=100;i++){
           if(i%2==0){
               System.out.println("Number is even"+i);
           }else{
               System.out.println("Number is odd"+i);
           }
       }
   }
  
   public static void main(String[] args) {
       String name;//String Variable
       Boolean choice = true;//Boolean Variable
      
       sc = new Scanner(System.in);
      
       System.out.println("Enter your name:");
       name = sc.next();
      
       System.out.println("Name entered by the user is:"+name);
//while loop usage
       while(choice){
           System.out.println("Please enter which operation you want to perform?");
           System.out.print("1.Print Fibonacci series 2.Calculate Factorial 3.Print Odd Even numbers upto 100 4.Exit");
          
           int nextInt = sc.nextInt();
          

//IF ELSE IF Ladder
           if(nextInt==1){
               System.out.print("Enter the number upto which fibonacci series needs to be generated");
               int fib=sc.nextInt();
               fibonnaci(fib);
           }else if(nextInt==2){
               System.out.print("Enter the number whose factorial is to be calculated");
               int fin=sc.nextInt();
               int factorial = factorial(fin);
               System.out.println("Factorial of the number"+fin+"is:"+factorial);
           }else if(nextInt==3){
               printOddEven();
           }else{
               System.exit(0);
           }
          
           System.out.println("Do you wish to continue.Type y or n");
          
           String next = sc.next();
          
           if(next.equalsIgnoreCase("n")||next.equalsIgnoreCase("N")){
               System.exit(0);
           }else if(next.equalsIgnoreCase("y")||next.equalsIgnoreCase("NY")){
               choice=true;
           }else{
               System.out.println("Invalid Input");
               System.exit(0);
           }
          
       }


  

   }

}


Related Solutions

Write an informal business report of your choice. The report must include all 3 sections of...
Write an informal business report of your choice. The report must include all 3 sections of an informal report - Introduction, discussion section and conclusion).
Writing a paper on The Product Life Cycle. The requirements of this paper include: Paper should...
Writing a paper on The Product Life Cycle. The requirements of this paper include: Paper should provide an in-depth analysis of the Product Life Cycle. Historical background. What value does the Product Life Cycle offer today's corporate managers. Within the corporate arena, discuss three types of strategies the management team may be planning or reviewing if they were to conduct a Product Life Cycle analysis? What are the advantages and disadvantages of this research tool? Each paper must include a...
Using the pseudocode found below, write only the actual (C++) code for this program. Include all...
Using the pseudocode found below, write only the actual (C++) code for this program. Include all libraries. Specification: Write a program that will repeatedly ask the user for quiz grades in the range from 0 to 20. Any negative value will act as a sentinel. When the sentinel is entered compute the average of the grades entered. Design: Constants None Variables float grade float total = 0 int count = 0 float average ------- Inital Algorithm Repeatedly ask user for...
Instructions: Write an informal business report of your choice. The report must include all 3 sections...
Instructions: Write an informal business report of your choice. The report must include all 3 sections of an informal report - Introduction, discussion section and conclusion).
Whats final project ideas for a electrical engineering major. Include all your work from your final...
Whats final project ideas for a electrical engineering major. Include all your work from your final project.
Write a program in a language of your choice to perform a search using the A*...
Write a program in a language of your choice to perform a search using the A* algorithm for the eight puzzle problem, in which numbers may be shifted one space at a time to transform the initial state into the goal state (see p. 103 – 3rd Ed., pp. 105-106 – 2nd Ed. of the text). 2. a) Use the start state-goal state combination given in pp. 103, Figure 3.28 (3rd Ed.), [pp. 105, Figure 4.7 (2rd Ed.)], as (start_1,...
Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: ·...
Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: · Your C++ source code file. (The file with the .CPP extension).No other files will be accepted. A screenshot of your program running. Program Instructions: Consider the following incomplete C++ program: #include int main() { … } 1. Write a statement that includes the header files fstream, string, and iomanip in this program. 2. Write statements that declare inFile to be an ifstream variable and...
Complete a final business plan that should include all sections of a business plan including any...
Complete a final business plan that should include all sections of a business plan including any appendices with resumes, and supporting documents for your business. Before compiling, The business should be presented as if you were presenting it to an investor.
Provide a layout of KFC that fulfills the critical elements below. Justify your reasoning for the...
Provide a layout of KFC that fulfills the critical elements below. Justify your reasoning for the important points mentioned. The audience for this document may be a manager, customer, or client related to the service or product provided by the organization. Consider that this document may need to be approved by your audience or by external sources such as government entities. Provide enough detail to give the viewer a good sense of the layout and its important aspects. The following...
Requirements for the assignment This assignment should be completed in essay format. It should include an...
Requirements for the assignment This assignment should be completed in essay format. It should include an introductory paragraph with a thesis statement, a body, and a concluding paragraph. It should be at least 1,000 words in length. Labor How are wages determined in the U.S.? What are the most significant trends in wages over the past five years? Your answer should address the following: Demand for labor Minimum Wage legislation Role of Unions Outsourcing of jobs Key points to be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT