Sie sind auf Seite 1von 10

Exercise 1: Discount Prices

During a special sale at a store, a 8% discount is taken on purchases over $10.00. Write a program that asks for the
amount of purchases, then calculates the discounted price. The purchase amount will be input in cents (as an
integer):

Enter amount of purchases: 2000

Discounted price: 1800

Use integer arithmetic throughout the program.

Inputs/ Outputs:

Program:

import java.util.Scanner;

public class Discount

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);

int amount, discount = 0 ;

System.out.println("Enter the purchase amount in cents: ");


amount = in.nextInt();
if(amount >= 1000)
{
discount = amount / 10;

System.out.println("Discounted Price= $" + (amount-discount));


}
else
System.out.println(" NO DISCOUNT");
}

Exercise 2 : Order Checker

Bob's Discount Bolts charges the following prices:

6 cents per bolt

3 cents per nut

1 cent per washer

Write a program that asks the user for the number of bolts, nuts, and washers in their purchase and then
calculates and prints out the total. As an added feature, the program checks the order. It is usually a mistake if
there are more bolts than nuts. In this case the program writes out "Check the Order." Otherwise the program
writes out "Order is OK." In either case the total price is written out.

Number of bolts: 12

Number of nuts: 8

Number of washers: 24

Check the Order

Total cost: 108

Use constants for the unit cost of each item. In other words, declare something like final int boltPrice = 5; and so
on in your program.

Inputs/ Outputs:
Program:

import java.util.Scanner;

class Checker
{
public static void main (String [] args)
{

Scanner in = new Scanner(System.in);

int bolts, nuts, washers;


double tbolts, tnuts, twashers, cost;

final double boltP = 0.6;


final double washP = 0.3;
final double nutP = 0.1;

System.out.println("Please enter the number of bolts");


bolts = in.nextInt();

System.out.println("Please enter the number of nuts.");


nuts = in.nextInt();

System.out.println("Please enter the number of washers");


washers = in.nextInt();

tbolts= bolts * boltP;


tnuts= nuts * nutP;
twashers= washers * washP;

cost= tbolts + tnuts + twashers;

if (bolts > nuts)


{

System.out.println("Total cost= $" + cost);


System.out.println("Check the order");

}
else
{

System.out.println("Order is OK");
System.out.println("Total cost= $" + cost);
}
}
}

Exercise 3: Last Chance Gas


Lisa's Last Chance Gas station sits on interstate 285 on the edge of Atanta, Georgia. There is no other gas station
for 200 miles. You are to write a program to help drivers decide if they need gas. The program asks for:

The capacity of the gas tank, in gallons

The indication of the gas gauge in percent (full= 100, three quarters full = 75, and so on)

The miles per gallon of the car.

The program then writes out "Get Gas" or "Safe to Proceed" depending on if the car can cross the 200 miles with
the gas remaining in the tank.

Tank capacity:12

Gage reading:50

Miles per gallon:30

Get Gas!

Use integers for all input and all arithmetic.

Inputs/ Outputs:
Program:

import java.util.Scanner;

public class Gas


{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int cap, per, mpg, distance, fuel;

System.out.println("Enter the tank capacity (in gallons): ");


cap = in.nextInt();

System.out.println("Enter the gauge reading (percent: 100%/75%/50%/25%): ");


per = in.nextInt();

System.out.println("Enter the miles per gallon of the car: ");


mpg = in.nextInt();

fuel = (cap * per) / 100;


distance = fuel * mpg;

if (distance < 200)


{
System.out.println("Get gas!");
}
else
{
System.out.println("Safe to Proceed!");

}
}
}
Exercise 4: Pie Eating Contest

At the State Fair Pie Eating Contest all contestants in the heavyweight division must weigh within 30 pounds of 250
pounds. Write a program that asks for a contestant's weight and then says if the contestant is allowed in the
contest.

Input/Output:

Programs:

import java.util.Scanner;

public class Pie


{

public static void main(String[] args)

Scanner in = new Scanner(System.in);


double weight;

System.out.println("Enter your weight in pounds, please: ");


weight = in.nextDouble();

if (weight >= 30 && weight <= 250)

System.out.println("You're allowed in the contest");


} else

System.out.println("You're NOT allowed in the contest");

Exercise 5: Ground Beef Value Calculator

Different packages of ground beef have different percentages of fat and different costs per pound. Write a
program that asks the user for:

The price per pound of package "A"

The percent lean in package "A"

The price per pound of package "B"

The percent lean in package "B"

The program then calculates the cost per pound of lean (non-fat) beef for each package and writes out which is the
best value.

Input/Output:
Program:

import java.util.Scanner;
public class packages
{
public static void main (String [] args)
{
Scanner in = new Scanner(System.in);

double priA, leA,priB, leB, perA, perB, totalA, totalB;

System.out.println("Price per pound package A:");


priA = in.nextDouble();
System.out.println("Percent lean package A:");
leA = in.nextDouble();
System.out.println("Price per pound package B:");
priB = in.nextDouble();
System.out.println("Percent lean package B:");
leB =in.nextDouble();

perA = 100-leA;
perB = 100-leB;
totalA = priA / leA * 100;
totalB = priB / leB * 100;

System.out.println("Package A cost per pound of lean:" + totalA);

System.out.println("Package B cost per pound of lean: " + totalB);

if(totalA < totalB)


{
System.out.println("package A is the best value");}

else {
System.out.println("package B is the best value");
}
}

Exercise 6: Y2K Problem Detector

Write a program that asks a user for their birth year encoded as two digits (like "62") and for the current year, also
encoded as two digits (like "99"). The program is to correctly write out the users age in years.

Input/Output:

Program:
import java.util.Scanner;
public class age
{
public static void main(String args [])
{
Scanner in = new Scanner(System.in);

int birth, curr, age, age2, age3, lowyr;

System.out.println("Birth year: ");


birth = in.nextInt();
System.out.println("Current year: ");
curr = in.nextInt();

age = curr-birth;

if(curr<20)
{
lowyr = curr + 2000;
age2 = lowyr - (birth+1900);
System.out.println("Your age:"+age2);}

else if (birth<20){
age3 = curr - birth;
System.out.println("Your age:" + age3); }
else {

System.out.println("Your age:" + age);}


}
}

Das könnte Ihnen auch gefallen