CSIS 10A Assignment 3 Due: 2/21 at midnight Read: Chapter 3 Choose and complete any 10 points from the problems below by first downloading the assignment 3 folder from the website. Use BlueJ to complete the assignment, then export JAR file and upload to the server using your pass code. You may do more than 10 points of work but the max award will be 11 points. Part I: Exam Prep Exercises--7 points total To be answered in the Exercises file Find the Error: in the Exercise file 1. Warning! This code contains ERRORS! if (x == 1); xxxxx there should be no ; because that makes the statment end, so it xxxxx will assign 2 to y no matter what y = 2; if (x == 2); xxxxx there should be no ; y = 3; if (x == 3); xxxxxx there should be no ; y = 4; 2. Warning! This code contains an ERROR! if (average = 100) xxxxx this should be if (average == 100) so it compares xxxxx average to 100, not assign 100 to average System.out.println("Perfect Average!");
Algorithm Workbench 1) if (y==0) x = 100; 2) if (y==10) x = 0; x = 1; if (amount1>10 && amount2<100) if (amount1 > amount2) System.out.println("the greater is " + amount1); System.out.println("the greater is " + amount2); if ( title1.compareto(title2) < 0) System.out.println(title1 + ", " + title2); System.out.println(title2 + ", " + title1); Part 3: Programming Challenges - 3 points each 1) Create a new class called P1BMI and solve the following:
P1BMI.java import java.util.scanner; public class P1BMI public static void main (String [] args) Scanner keyboard = new Scanner(System.in); double weight, height; //declare variables //prompt user and get weight and height System.out.println("What is your weight in pounds?"); weight = keyboard.nextdouble(); System.out.println("What is your height in inches?"); height = keyboard.nextdouble(); //calculate their bmi double bodymass = weight * 703 / (height * height); //tell user if they are underweight, overweight or optimal if (bodymass < 18.5) System.out.println("You are underweight."); if (bodymass > 25) System.out.println("You are overweight."); System.out.println("You have optimal weight."); 2) Create a new class called P2SortedNames P2SortedNames.java
/** * Asks the user to enter three names, then displays them in alphabetical order. * * Andrew Fiola * 2012.02.16 */ import java.util.scanner; public class P2SortedNames public static void main(string[] args) //declare variables Scanner keyboard = new Scanner(System.in); //keyboard scanner String name1; //Names entered by user String name2; String name3; //Prompt user for input System.out.print("\fPlease enter a name: "); name1 = keyboard.nextline(); System.out.print("And another: "); name2 = keyboard.nextline(); System.out.print("And one more: "); name3 = keyboard.nextline(); //Display those names, alphabetically sorted //Sort by first determining which name is first, //then determining the order of the second two. System.out.println("Alphabetically sorted:"); if (name1.comparetoignorecase(name2) <= 0 && name1.comparetoignorecase(name3) <= 0) if (name2.comparetoignorecase(name3) <= 0) if (name2.comparetoignorecase(name1) <= 0 && name2.comparetoignorecase(name3) <= 0) if (name1.comparetoignorecase(name3) <= 0) if (name1.comparetoignorecase(name2) <= 0)
3) Create a new class called P3InternetBill and solve the following: P3InternetBill.java //Dana A. DeVost //18FEB2012 /** * Write a description of class P3InternetBill here. This program is going to ask for two user inputs, type of package and hours used. * Then calculate the final cost for the package. */ import java.util.scanner; import java.text.decimalformat; public class P3InternetBill public static void main (String [] args) //Create a DecimalFormat object with 2 decimal places DecimalFormat formatter = new DecimalFormat("#0.00"); //Create a Scanner object to read input Scanner keyboard = new Scanner (System.in); String typeofpackage; double cost; double hoursused; //Letter of package bought //Cost of typeofpackage plus extra hours //Number of hours used. System.out.println("We provide three types of Internet Service: \n" + "Package A: For $9.95 per month, 10 hours of access are provided. " + "Additional hours are $2.00 per hour.\n" + "Package B: For $13.95 per month, 20 hours of access are provided. " + "Additional hours are $1.00 per hour.\n" + "Package C: For $19.95 per month, unlimited access is provided.\n"); System.out.println("Enter A, B, or C, for your package type: "); typeofpackage=keyboard.nextline(); System.out.println("Enter approximately how many hours of service were used. "); hoursused = keyboard.nextint(); if ((typeofpackage.equals("a") typeofpackage.equals("a")) && (hoursused <=10)) System.out.println("Your monthly cost is $9.95. ");
if ((typeofpackage.equals("a") typeofpackage.equals("a")) && (hoursused >10)) cost = (((hoursused-10) * 2.00) + (9.95)); System.out.println("Your monthly cost is: $ " + cost); if ((typeofpackage.equals("b") typeofpackage.equals("b")) && (hoursused <=20)) System.out.println("Your monthly cost is: $13.95. "); if ((typeofpackage.equals("b") typeofpackage.equals("b")) && (hoursused >20)) cost = ((hoursused-20) * 1.00) + (13.95); System.out.println("Your monthly cost is: $ " + cost); if (typeofpackage.equals("c") typeofpackage.equals("c")) System.out.println("Your bill is $19.95 a month with unlimited access. ");