WIT COMP1000 Exam 2 Review Wentworth Institute of Technology - - PowerPoint PPT Presentation

wit comp1000
SMART_READER_LITE
LIVE PREVIEW

WIT COMP1000 Exam 2 Review Wentworth Institute of Technology - - PowerPoint PPT Presentation

Wentworth Institute of Technology Engineering & Technology WIT COMP1000 Exam 2 Review Wentworth Institute of Technology Engineering & Technology Format The exam will be 5-6 problems, with some problems having multiple sub-questions


slide-1
SLIDE 1

Wentworth Institute of Technology

Engineering & Technology

WIT COMP1000

Exam 2 Review

slide-2
SLIDE 2

WIT COMP1000

2

Wentworth Institute of Technology

Engineering & Technology

  • Do. Learn. Succeed.

Format

§ The exam will be 5-6 problems, with some problems having multiple sub-questions § You are allowed a single 8.5x11" piece of paper with whatever notes you want on it

» Can be handwritten or computer printed » You may use both the front and back

§ No calculators, books, laptops, phones, or anything besides your single page of notes may be used

slide-3
SLIDE 3

WIT COMP1000

3

Wentworth Institute of Technology

Engineering & Technology

  • Do. Learn. Succeed.

Format

§ Kinds of questions to expect:

» Explain a program or part of a program » Translate between "normal" math expressions and their Java equivalents » Write your own code » Fix incorrect code / find bugs in code » Fill in the blank (in a program) » Short answer

slide-4
SLIDE 4

WIT COMP1000

4

Wentworth Institute of Technology

Engineering & Technology

  • Do. Learn. Succeed.

Content

§ Everything we've covered so far in the semester, including:

» Input and output » Mathematical expressions (order of operations, integer division, etc) » if-else statements » while and do-while loops » for loops » Methods » Arrays

slide-5
SLIDE 5

WIT COMP1000

5

Wentworth Institute of Technology

Engineering & Technology

  • Do. Learn. Succeed.

§ The following slides contain exercises that will help you prepare for the exam § These exercises are all about writing code to help remind you of the things we've done so far this semester § Refer back to the exam 1 review slides (and your actual exam) if you need a reminder of the style of questions

Review Exercises

slide-6
SLIDE 6

WIT COMP1000

6

Wentworth Institute of Technology

Engineering & Technology

  • Do. Learn. Succeed.

Exercise

§ Write a program that reads in a series of positive integers and prints out the maximum value entered. The user will indicate they are finished entering numbers by entering zero or a negative integer.

slide-7
SLIDE 7

WIT COMP1000

7

Wentworth Institute of Technology

Engineering & Technology

  • Do. Learn. Succeed.

Answer

Scanner input = new Scanner(System.in); int inputValue; int max = 0; System.out.print("Enter positive integers, stopping with zero or a negative number: "); do { inputValue = input.nextInt(); if(inputValue > max) { max = inputValue; } } while(inputValue > 0); if (max == 0) { System.out.println("You didn't enter any positive numbers!"); System.exit(0); } System.out.printf("The max was %d%n", max);

slide-8
SLIDE 8

WIT COMP1000

8

Wentworth Institute of Technology

Engineering & Technology

  • Do. Learn. Succeed.

Exercise

§ Write a program that uses a for loop to calculate N!, given N. Ask the user for a value

  • f N and your program should compute and

print the value of N! ( = 1 * 2 * 3 * … * N).

slide-9
SLIDE 9

WIT COMP1000

9

Wentworth Institute of Technology

Engineering & Technology

  • Do. Learn. Succeed.

Answer

Scanner input = new Scanner(System.in); int n; int factorial = 1; System.out.print("Enter N: "); n = input.nextInt(); if (n < 0) { System.out.println("No factorial for negative numbers!"); System.exit(0); } for (int i = 1; i <= n; i++) { factorial = factorial * i; } System.out.printf("%d! = %d%n", n, factorial);

slide-10
SLIDE 10

WIT COMP1000

10

Wentworth Institute of Technology

Engineering & Technology

  • Do. Learn. Succeed.

Exercise

§ Write a method that computes the gravitational force between two bodies using the formula:

» m1 is the mass of the first body » m2 is the mass of the second body » d is the distance between them » G is a constant: 6.673 x 10-11 N(m/kg)2

§ Both masses and the distance must be passed as arguments to the method § Also write a main() method to test your method

𝐺=​𝐻𝑛1𝑛2/𝑒2

slide-11
SLIDE 11

WIT COMP1000

11

Wentworth Institute of Technology

Engineering & Technology

  • Do. Learn. Succeed.

Answer

public class ClassExamples { static final double G = 6.673 * Math.pow(10, -11); public static void main(String[] args) { double f = gravitationalForce(10000, 10000, 1); System.out.printf("Given m1=%.3fkg, m2=%.3fkg, d=%.3fm, then F=%.3fN%n", 10000.0, 10000.0, 1.0, f); } public static double gravitationalForce(double m1, double m2, double d) { double force; force = (G * m1 * m2) / (d * d); return force; } }

slide-12
SLIDE 12

WIT COMP1000

12

Wentworth Institute of Technology

Engineering & Technology

  • Do. Learn. Succeed.

Exercise

§ Write a program that asks the user for exactly ten integers and then prints them out in the reverse order given. Use an array to store the values so you can print them out after you have read in all ten.

slide-13
SLIDE 13

WIT COMP1000

13

Wentworth Institute of Technology

Engineering & Technology

  • Do. Learn. Succeed.

Answer

Scanner input = new Scanner(System.in); int[] values = new int[10]; int i; System.out.print("Enter 10 integers: "); for (i = 0; i < values.length; i++) { values[i] = input.nextInt(); } System.out.println("The values in reverse order: "); for (i = values.length-1; i >= 0; i--) { System.out.println(values[i]); }

slide-14
SLIDE 14

WIT COMP1000

14

Wentworth Institute of Technology

Engineering & Technology

  • Do. Learn. Succeed.

Wrap Up

§ Review the previous slides and assignments § Work through all the examples and exercises § Use the page of notes as a study guide to help you prepare for the exam § Come see me with any questions or if you need some help understanding anything we've covered so far this semester