Create a class called oddintegers with a main method

Assignment Help JAVA Programming
Reference no: EM13963167

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.

Reference no: EM13963167

Questions Cloud

Explain the ideas and details presented in the transcript : Read "6 ways to Manage Overload," Transcript below then explain the ideas and details presented in the transcript below
Cash balance of the firm changed : 1.  Lube Oil, in Question 3, paid a dividend of $20 million and bought back $25 million in stock. Estimate how much the cash balance of the firm changed during the year.
How monopoly exploitation is reduced by regulation : The residual of sales revenue minus the explicit accounting costs of doing business.
What is the value of helping a client change : What is the value of helping a client change his or her daily routine? Would you help a client establish a new routine based on his or her rehab program? Why or why not
Create a class called oddintegers with a main method : 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.
Steps a client can take to manage their challenges : Write a 1,050- to 1,400-word paper on the role that family, shame, and multicultural issues play in aftercare, how can they support or undermine a person's recovery, and the steps a client can take to manage their challenges
How long will the trip take as measured on earth : A swimmer wants to swim straight across a river with current flowing at a speed of v1 = 0.27 km/h. If the swimmer swims in still water with speed v2 = 0.81 km/h, at what angle should the swimmer point upstream from the shore and at what speed v w..
Percent of cash returned to stockholders : 1. Stock buybacks really do not return cash to stockholders, because only those who sell back stock receive the cash. Is this statement true or false? Explain. 2. Between 1988 and 2008, we saw an increase in the percent of cash returned to sto..
Summarize the characteristics of process evaluations : Summarize the characteristics of process evaluations. Provide two examples of process evaluations. Make up your own examples using the new scenario program/activity you have developed

Reviews

Write a Review

JAVA Programming Questions & Answers

  Implement a thread for each creature representing a task

Implement a threads and a GUI interface using advanced Java Swing classes.

  Program to calculate the average of 5 numbers entered.

Show what the PC monitor will show when the program is executed with the following values used: 27, 12, and 15.

  Modify the java application using java netbeans ide

Modify the Java application using Java NetBeans IDE to meet these additional and changed business requirements. The application will now compare the total annual compensation of at least two salespersons.

  Threads and synchronization

Threads and Synchronization- Whenever we have multiple entities working together to get something done, we have the problem of synchronization between the entities

  How do i display a message in the browsers status bar

How do I display a message in the browser's status bar? What are the main differences between applets and applications?

  Design an application for pizza order process

Create an application to take and procedure a pizza order. The user should be able to make pizza order choices from listboxes, and the application should show the order price.

  Design a test program that creates a car object

Also design a test program that creates a Car object, then calls the accelerate method five times, and after each call gets the current speed of the car and displays it. Then it calls the brake method five times, and after each call gets the curre..

  Compute the total annual compensation of a salesperson

Write a Java application using NetBeans Integrated Development Environment (IDE) that calculates the total annual compensation of a salesperson. Consider the following factors: A salesperson will earn a fixed salary of $35,750

  Provide java code to create and initialize an array

Provide Java code to create and initialize an array of integers (Java primitive int), floats or any other Java primitive type of your choice. You pick the array name and length.

  Create a frame-based application that displays an array

Include a TextField in the South. When the user clicks a button, the course name should display in the TextField

  Uses a 2-d array to store the highest and lowest temperature

Write a program that uses a 2-D array to store the highest and lowest temperatures for each montjh of the year. The program should output the average high, average low, and highest and lowest temperatures of the year. Your program must consist of ..

  Need usual internal node structure and constructor

Create a basic, generic DataTree class. As described, it will consist of a binary tree structure, and will need the usual internal node structure and constructor -  Write the other methods used in Section One of the commented-out test code in Main..

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd