Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

Decision Control Structures

Set-Up

  • Create a new project in your Eclipse workspace named:
  • In thesrcfolder, create a package named:
  • Import into this new package all of the following .java files
    • DivideTwo.java

Part 1 - if/else Statement

Open the file DivideTwo.java.  The Java source code is shown below:

package edu.ilstu;

import java.util.Scanner;

 

public class DivideTwo

{

     public static void main(String[] args)

     {   

           Scanner keyboard = new Scanner(System.in);

           int numerator = 0;

           int denominator = 0;

           double quotient = 0;

 

           System.out.println("This program divides two numbers.");

           System.out.print("Enter the numerator:  ");

           numerator = keyboard.nextInt();

 

           System.out.print("Enter the denominator:  ");

           denominator = keyboard.nextInt();

           quotient = (double) numerator / denominator;

 

           System.out.println(numerator + "/" + denominator

+ " = " + quotient);

 

           keyboard.close();   

}

}

Compile and run DivideTwo.java, and observe the output.  The program prompts the user for two integers that represent the numerator and the denominator of a fraction.  The Scanner object named keyboard is used to read the integers provided by the user.

Modify the program to include an if/else statement to check for division by zero.  If the denominator is not equal to zero, display the result of the division, otherwise display a message to the user that division by zero is not allowed.

Part 2 - Switch Statement

Open the provided file SwitchErrors.java.  The Java source code is shown below.  The program evaluates an integer entered by the user and displays the color assigned to the integer.  Compile the program.  The program has several syntax and logic errors.  Fix the syntax errors and compile and run the program. Does the program run as you expected?  Locate and correct the errors in the program logic.  Be sure to make use of the debugger available in Eclipse to help identify errors.

package edu.ilstu;

import java.util.Scanner;

publicclass SwitchErrors

{

     publicstaticvoid main(String[] args)

{

           Scanner keyboard = new Scanner(System.out);

 

           System.out.println("Key:  1=blue, 2=red, 3=green");

           System.out.print("Enter a number and I'll return ");

           System.out.print(" the corresponding color.  ");

           number = keyboard.nextInt();

 

           switch(number)

           {

                case 1:

                     System.out.println("You chose red!");

                     break;

                case 2:

                     System.out.println("You chose blue!");

                     break;

                case 3:

                     System.out.println("You chose green!");

                default:

                     System.out.println("Color not available!");

                     break;    

           }

     }

}

Part 3 while Loop

Open the provided file ConvertLoop.java

Convert the following code so that it uses nested while statements instead of for statements.  Place the new code at the end of the code in ConvertLoop.java.

int s = 0;

int t = 1;

 

for (int i = 0; i < 5; i++)

{

     s = s + i;

     for (int j = i; j > 0; j--)

     {

           t = t + (j-1);

     }

     s = s + t;

     System.out.println("T is " + t);

}

System.out.println("S is " + s);

Part 4 - Loops from scratch

Create a class called OddIntegers with a main method.  Write the code that will compute the sum of the first n positive odd integers.  For example, if n is 5 you should compute 1 + 3 + 5 + 7 + 9.  Read the value forn from the user and display the result to the screen with an appropriate label.

Java, Programming

  • Category:- Java
  • Reference No.:- M91631188
  • Price:- $50

Priced at Now at $50, Verified Solution

Have any Question?


Related Questions in Java

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Simple order processing systemquestion given the classes

Simple Order Processing System Question: Given the classes Ship (with getter and setter), Speedboat, and SpeedboatTest. Answer the following questions: Refine the whole application (all classes) and create Abstract class ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Question slideshows or carousels are very popular in

Question : Slideshows (or carousels) are very popular in websites. They allow web developers to display news or images on the website in limited space. In this code challenge, you are required to complete the JavaScript ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Part a specification - robot simulationpart a

PART A Specification - Robot Simulation PART A Requirements To complete this assignment you will use the supplied eclipse project Robot P1/. It is already set up to execute a simple arm movement loop which you will build ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As