chortle programming chapter 13 exercise answers

6
import java.util.Scanner ; class Delicatessen { public static void main (String[] args) { Scanner scan = new Scanner( System.in ); int price, overnight, shipping, total ; String item; System.out.print("Enter the item: "); item = scan.nextLine(); System.out.print("Enter the price (in pennies): "); price = scan.nextInt(); System.out.print("Overnight delivery (0==no, 1==yes): "); overnight = scan.nextInt(); System.out.println("Invoice:"); System.out.println( item + " " + (price/100) + "." + (price%100)); if ( price < 1000 ) { shipping = 200; } else { shipping = 300; } if ( overnight == 1) { shipping = shipping+500; } System.out.println("Shipping " + ((overnight+shipping)/100) + ".00");

Upload: bowlinmaster

Post on 10-Apr-2015

791 views

Category:

Documents


2 download

DESCRIPTION

My answers for the Chapter 13 Chortle Programming exercises.

TRANSCRIPT

Page 1: Chortle Programming Chapter 13 Exercise Answers

import java.util.Scanner ;class Delicatessen{ public static void main (String[] args) { Scanner scan = new Scanner( System.in ); int price, overnight, shipping, total ; String item;

System.out.print("Enter the item: "); item = scan.nextLine();

System.out.print("Enter the price (in pennies): "); price = scan.nextInt();

System.out.print("Overnight delivery (0==no, 1==yes): "); overnight = scan.nextInt();

System.out.println("Invoice:");System.out.println( item + " " + (price/100) + "." + (price%100));

if ( price < 1000 ){

shipping = 200;}

else{

shipping = 300;}

if ( overnight == 1){

shipping = shipping+500;}System.out.println("Shipping " + ((overnight+shipping)/100) + ".00");System.out.println("Total cost: " + (price+shipping)/100 + "." + (price+shipping)

%100); }}

Page 2: Chortle Programming Chapter 13 Exercise Answers

import java.util.Scanner;

class SteamEngine{ public static void main (String[] args) { Scanner scan = new Scanner( System.in ); double tair, tsteam, efficiency;

System.out.println("Air Temperature:"); tair = scan.nextDouble(); System.out.println("Steam Temperature:"); tsteam = scan.nextDouble(); if ( tsteam < 373 ) efficiency = 0; else { efficiency = 1-(tair/tsteam); } System.out.println("Efficiency is: " + efficiency + "%"); }}

Page 3: Chortle Programming Chapter 13 Exercise Answers

import java.util.Scanner;class Microwave{ public static void main (String[] args) { Scanner scan = new Scanner( System.in ); double heattime, items, heattimerecom; System.out.println("How many items are you heating?"); items = scan.nextDouble();

if ( items == 1) { System.out.println("What is the heat time in seconds?"); heattime = scan.nextDouble(); heattimerecom = heattime; System.out.println("Recommended heating time is " + heattimerecom + " seconds."); } if ( items == 2) { System.out.println("What is the heat time in seconds?"); heattime = scan.nextDouble(); heattimerecom = (heattime*1.5); System.out.println("Recommended heating time is " + heattimerecom + " seconds."); } if ( items == 3) { System.out.println("What is the heat time in seconds?"); heattime = scan.nextDouble(); heattimerecom = heattime*2.0; System.out.println("Recommended heating time is " + heattimerecom + " seconds."); } if ( items > 3) { System.out.println("Heating more than three items at once is not recommended."); } }}

Page 4: Chortle Programming Chapter 13 Exercise Answers

import java.util.Scanner ;class Fantasy{ public static void main (String[] args) { Scanner scan = new Scanner( System.in ); int strength, health, luck; String name; System.out.println("Welcome to the Epic Quest"); System.out.println("Enter the name of your character: "); name = scan.nextLine();

System.out.print("Enter Strength (1-10): "); strength = scan.nextInt();

System.out.print("Enter Health (1-10): "); health = scan.nextInt();

System.out.print("Enter Luck (1-10): "); luck = scan.nextInt();

if ( (strength + health + luck ) > 15 ) { strength = 5; health = 5; luck = 5; System.out.println("You have give your character too many points! Default values have been assigned:"); }

System.out.println( name + ", Strength: " + strength + ", Health: " + health + ", Luck: " + luck); }}