Introduction to Information and Computer Science: Computer Programming Application Activities, Instructor Guidelines, and Expected Outcomes 1. Select a programming language and research its development and use. When was it created and by whom? For what purpose was it created? What programming paradigm(s) does it support? What applications use the language? Instructor Guidelines: You could assign each student a programming language to ensure a diverse set of languages. As a class, you can discuss which programming languages are popular today, which ones are not, and why. Expected Outcome(s): Become familiar with the details of one programming language. Objective(s): 1, 2 Lecture(s)/Slide(s): a14 - 20 2. Write an algorithm that describes the process you follow for preparing for an exam. Include as many details as you can, including things you do throughout the term and others that aren’t directly related to the course (i.e. getting a good night’s sleep). What step(s) do you think would appear in others’ algorithms? Which one(s) do you think are unique to yours? Instructor Guidelines: Discussing their answers in class is a good way for them to recognize the similarities and differences between their algorithms. Which things vary: the steps of the process or the amount of detail provided in the algorithm? Expected Outcome(s): Writing a detailed algorithm. Possible Answer: 1. Attend class and take detailed notes. 2. Ask questions when confused. 3. Complete homework on time. 4. Make flashcards the week before the exam. 5. Review flashcards the night before the exam. 6. Get a good night’s sleep the night before the exam. 7. Eat a good meal before the exam. 8. Wear my lucky shirt. 9. Look at my flashcards right before the exam. Objective(s): 1 Lecture(s)/Slides(s): a / all Health IT Workforce Curriculum Version 3.0 / Spring 2012 Introduction to Information and Computer Science Computer Software This material (Comp4_Unit5) was developed by Oregon Health and Science University funded by the Department of Health and Human Services, Office of the National Coordinator for Health Information Technology under Award Number IU24OC000015. 1 3. Using the Java tutorials listed in the additional resources, write a “Hello, World!” program using the editor/IDE (Integrated Development Environment) of your choice. Instructor Guidelines: The comp4_unit5_java_resources.doc file has a list of java resources, including development environments and tutorials. Students should be able to follow one of the tutorials to produce a HelloWorld program. Expected Outcome(s): Gain experience with installing a programming editor/IDE/compiler and writing a simple program to demonstrate that it works. Answer: (also found in HelloWorld.java) public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } Objective(s): 3, 4 Lecture(s)/Slide(s): c / all 4. Modify the “Hello, World!” program from exercise 3 to ask the user how many times to print “Hello, World!” and then output it that many times. Instructor Guidelines: Students should be able to use examples from the lectures to do input, output and a loop. There are many online resources that should be helpful. Expected Outcome(s): Gain experience with input and output and loops. Answer: (also found in HelloWorld2.java) Note: this program can be done with a while loop or a do while loop as well. import java.util.*; public class HelloWorld2 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int numHellos; int i; System.out.println("How many times do you want me to say hello?"); numHellos = keyboard.nextInt(); Health IT Workforce Curriculum Version 3.0 / Spring 2012 Introduction to Information and Computer Science Computer Software This material (Comp4_Unit5) was developed by Oregon Health and Science University funded by the Department of Health and Human Services, Office of the National Coordinator for Health Information Technology under Award Number IU24OC000015. 2 for (i = 0; i < numHellos; i++) { System.out.println("Hello, World!"); } } } Objective(s): 4 Lecture(s)/Slide(s): d / all 5. Modify the code given in Lecture(s)/Slide(s): 3, slide 18 to calculate BMI for height entered in inches and weight entered in pounds. (The code is also provided in the CalcBMI.java file). First, modify the algorithm and then the code. Can you think of more than one way to do this? Instructor Guidelines: Part of this assignment is problem solving—how do you convert a formula in kilograms and meters to lbs and inches? They can look up the new formula online or they can derive it using the conversion formulas: 1 kilogram = 2.20462262 pounds 1 meter = 39.3700787 inches Expected Outcome(s): Gain experience with algorithms, assignment statements and expressions. Answer: Algorithm: 1. Prompt user for weight in lbs 2. Read in weight 3. Prompt user for height in inches 4. Read in height 5. Calculate BMI 6. BMI = (weight/(height * height))*703 7. Output BMI (also found in CalcBMILbsIn.java) import java.util.*; public class CalcBMILbsIn { public static void main(String[] args) { double bmi, weight, height; Scanner keyboard = new Scanner(System.in); Health IT Workforce Curriculum Version 3.0 / Spring 2012 Introduction to Information and Computer Science Computer Software This material (Comp4_Unit5) was developed by Oregon Health and Science University funded by the Department of Health and Human Services, Office of the National Coordinator for Health Information Technology under Award Number IU24OC000015. 3 System.out.println("Welcome to the BMI calculator"); System.out.println("Enter weight in lbs"); weight = keyboard.nextDouble(); System.out.println("Enter height in inches"); height = keyboard.nextDouble(); bmi = (weight/(height*height))*703; System.out.print("BMI is "); System.out.println(bmi); } } You could also convert the height to m and the weight to kg and use the original BMI formula. Objective(s): 4 Lecture(s)/Slide(s): c / all 6. Write the Java code for the BMICalculator class given in the UML diagram in Lecture(s)/Slide(s): 5, slide 7. Here is an example class that would use this object. Note that it is similar to the code given in Lecture(s)/Slide(s): 4, but uses a BMICalculator object instead. (The code is also provided in the CalcBMI2.java file). Use the CalcBMI2 class to test your BMICalculator class (put both in the same directory/folder, compile them and run CalcBMI2). import java.util.*; public class CalcBMI2 { public static void main(String[] args) { double weight, height; int anotherBMI; //create a BMICalculator object BMICalculator bmiObject = new BMICalculator(); Scanner keyboard = new Scanner(System.in); System.out.println("Welcome to the BMI calculator"); anotherBMI = 1; while (anotherBMI == 1) { System.out.println("Enter weight in kg"); weight = keyboard.nextDouble(); //set the weight in the bmiObject bmiObject.setWeight(weight); System.out.println("Enter height in m"); height = keyboard.nextDouble(); //set the height in the bmiObject bmiObject.setHeight(height); Health IT Workforce Curriculum Version 3.0 / Spring 2012 Introduction to Information and Computer Science Computer Software This material (Comp4_Unit5) was developed by Oregon Health and Science University funded by the Department of Health and Human Services, Office of the National Coordinator for Health Information Technology under Award Number IU24OC000015. 4 //call the methods in the bmiObject for outputting BMI //and the BMI category bmiObject.calcBmi(); bmiObject.outputBmi(); bmiObject.outputBmiCategory(); System.out.println("Do you want to calculate another?”); System.out.println("Enter 1 for yes and 0 for no"); anotherBMI = keyboard.nextInt(); } System.out.println("Good Bye!"); } } Instructor Guidelines: This activity may be a bit advanced for students with no programming experience. Working in groups can help alleviate this problem, especially if groups have good balance of the degree of computer background. Expected Outcome(s): Gain experience with programming with objects. Answer: (also found in BMICalculator.java) public class BMICalculator { private double height, weight; private double bmi; public void setHeight(double pHeight) { height = pHeight; } public void setWeight(double pWeight) { weight = pWeight; } public void calcBmi() { bmi = weight/(height*height); } public void outputBmi() { System.out.print("The BMI is "); System.out.println(bmi); } public void outputBmiCategory() { if (bmi < 18.5) System.out.println("Underweight"); Health IT Workforce Curriculum Version 3.0 / Spring 2012 Introduction to Information and Computer Science Computer Software This material (Comp4_Unit5) was developed by Oregon Health and Science University funded by the Department of Health and Human Services, Office of the National Coordinator for Health Information Technology under Award Number IU24OC000015. 5 else if ((bmi >= 18.5) && (bmi < 25.0)) System.out.println("Normal weight"); else if ((bmi >= 25.0) && (bmi < 30.0)) System.out.println("Overweight"); else System.out.println("Obese"); } } Objective(s): 5 Lecture(s)/Slide(s): e / all 7. What if a user enters 0 for the height in Exercise 6? What will happen when the BMI is calculated? How can we prevent this from happening? Instructor Guidelines: Division by zero is a runtime error that will crash a program. If students aren’t familiar with this error, you can have them program a simple program that divides by zero to see what happens. Also, checking for error conditions practices using if statements. Expected Outcome(s): Think about error prevention and encapsulation. Answer: If height is 0, then there will be a division by zero in the BMI calculation. We can prevent this in a couple of ways. We can print an error message in the setHeight method before height is going to be set to 0. We can also check in the calcBMI method for height equal to 0 before performing the division. In both cases, we put the error check in the BMICalculator class so that any other code that uses this class doesn’t need to worry about checking for height equal to 0 (encapsulation). Objective(s): 5 Lecture(s): e / all Health IT Workforce Curriculum Version 3.0 / Spring 2012 Introduction to Information and Computer Science Computer Software This material (Comp4_Unit5) was developed by Oregon Health and Science University funded by the Department of Health and Human Services, Office of the National Coordinator for Health Information Technology under Award Number IU24OC000015. 6