Docsity

Review for Midterm Exam - Computer Programming I | CS 121, Study notes of Computer Science

Toggle sidebar
bg1
CS121/IS232
Review for Midterm
1. Expressions – Review worksheet
Problems occur with integer and double division, left to right association, and casts.
25 / 4 = 6 but 25 / 4.0 = 6.25
25 / 4 * 2 = 6 * 2 = 12
double (25 / 4) = 6.0 but 25 / double (4) = 6.25
2. Identifiers
Identifiers begin with a letter, which may be followed by any number of letters and digits.
They may also have one underscore character.
3. Print statements
System.out.print (…) prints the contents of the parentheses and stays on the same line.
System.out.println (…) prints the contents of the parentheses and goes to the next line.
A plus (+) sign is used to concatenate two strings so that one is attached to the end of the
other.
4. If – else statement
if (condition)
Statement;
else
Statement;
The Statement may also be an if-else statement. You can put them together in many different
ways.
Compound statements use && (and), || (or), and ! (not).
if (birthmonth == month && birthday > day) age = age – 1;
5. Short answers
Review the lecture sheets. There may be questions on datatypes, classes, methods,
constructors, etc.
6. Syntax errors
One question with two classes and a lot of syntax errors. Find the errors, circle them, and
indicate what is wrong.
7. Short assignments and print statements
Questions like “Write a statement that will print out your animal’s name and age.”
pf2

Partial preview of the text

Download Review for Midterm Exam - Computer Programming I | CS 121 and more Study notes Computer Science in PDF only on Docsity!

CS121/IS

Review for Midterm

  1. Expressions – Review worksheet Problems occur with integer and double division, left to right association, and casts. 25 / 4 = 6 but 25 / 4.0 = 6. 25 / 4 * 2 = 6 * 2 = 12 double (25 / 4) = 6.0 but 25 / double (4) = 6.
  2. Identifiers Identifiers begin with a letter, which may be followed by any number of letters and digits. They may also have one underscore character.
  3. Print statements System.out.print (…) prints the contents of the parentheses and stays on the same line. System.out.println (…) prints the contents of the parentheses and goes to the next line. A plus (+) sign is used to concatenate two strings so that one is attached to the end of the other.
  4. If – else statement if (condition) Statement; else Statement; The Statement may also be an if-else statement. You can put them together in many different ways. Compound statements use && (and), || (or), and! (not). if (birthmonth == month && birthday > day) age = age – 1;
  5. Short answers Review the lecture sheets. There may be questions on datatypes, classes, methods, constructors, etc.
  6. Syntax errors One question with two classes and a lot of syntax errors. Find the errors, circle them, and indicate what is wrong.
  7. Short assignments and print statements Questions like “Write a statement that will print out your animal’s name and age.”

Questions 8, 9, and 10. Program with parts to write. You are to fill in the code in the three methods in the Panda class that are empty. The question will not be the same as the one below, but it will be similar. package zoo; // An application that calculates a panda’s body mass index. public class ZooKeeper { public static void main(String[] args) { Panda panda = new Panda (); panda.getData (); panda.calculateBMI (); panda.displayData (); } // main } // ZooKeeper package zoo; import java.util.Scanner; public class Panda { private String name; private double weight, height, bmi; Scanner scan = new Scanner (System.in); public void getData () { } // getData public void calculateBMI () { } // calculateBMI public void displayData () { } // displayData } // Panda