Java Programming Assignment 13

download Java Programming Assignment 13

of 2

description

Java Programming

Transcript of Java Programming Assignment 13

Intro to JavaMr. Weisswange

Java Programming Assignment #1

Since this is your first assignment, here's how you are going to handle the setup:

1. In NetBeans, right-click on Source Packages and select NewJava Package.2. Name your package assignment1 and click Finish.3. For each problem, right-click on your assignment1 package and select NewJava Main Class.4. Name each class after the problem, such as Problem1_1 or AsciiArt.5. Write each program in its own class file.

When you are done, you'll need to copy all of your code into a single text file in order to upload it to Edmodo. To do this, open Notepad and copy the text of each source code file, in order, from NetBeans into Notepad. Finally, save your Notepad text file and upload that file to Edmodo.

1. ASCII art

Since the output window of NetBeans is in a monospace font, you can use text (ASCII) characters to print a picture. Use System.out.println() statements to print a picture of your own devising, such as: ^ \|/ / \ -O- / \ /|\ / \

| || +-+ || | | || | | |+--+-+--+-----------, only better.

2. Sum of consecutive integers

Write a program which asks the user for two integers and prints the sum of all the integers from one to the other, inclusive, using the formula

where a is the smaller integer and b is the larger integer.

This program will print the sum of consecutive integers.Enter the smaller integer: 10Enter the larger integer: 15The sum is 75.

(The boldface text indicates values that are entered by the user.)

3. Mileage comparison

Write a program which asks the user for the number of miles driven by two cars and the number of gallons of gasoline used by each. Then, it prints which car gets the better mileage, or that the mileages are the same (this will require 3 if statements).

This program will compare the mileages of two cars.Enter the miles driven by the first car: 250Enter the gallons used by the first car: 10Enter the miles driven by the second car: 300Enter the gallons used by the second car: 11The second car has the better mileage.

Right now, we are only working in integers, so the miles and gallons will have to be integer values. This does pose a bit of a problem with calculation. The comparison we want to make is between two ratios. That means that car 1 will have the better mileage if

,

where and represent the miles driven by cars 1 and 2, and and represent the gallons used by cars 1 and 2. This presents a problem, since we are using integers and cannot handle fractions or decimals, which would often result from the division.

However, we can fix this problem. Since both and are positive numbers, we can simply cross-multiply the inequality. Therefore, Car 1 will have the better mileage if

,where both sides are integers.

The comparisons that you need to use for the cases where Car 2 has better mileage and for equality can be found simply by changing the comparison operator used above.