




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: Introduction to Computing - Java; Subject: Computer Science; University: North Carolina State University; Term: Spring 2005;
Typology: Exams
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Name: ________________________________________________________________
Lab # and/or Lab Instructor: ________________________________________ (1 point)
This exam is worth 100 points. You may use your personal copy of the Wu textbook
and your personal notes. You many not use any other materials, including your
neighbor. You may not share your textbook, notes, or calculators.
Syntax 1. Structure or pattern of a programming language.
Instance variables 2. Used to define the states or data of an object.
Class 3. A template of something you are trying to represent.
Constructor 4. Initializes an object when it is created.
Source code 5. Human readable code.
Methods 6. Used to define actions or behaviors of an object.
Integers or ints 7. A primitive data type for numbers that does not have a fractional
part.
Object 8. An instance of a class
Keyword 9. A word that has special meaning in a programming language
and cannot be used by programmers as a name for classes, methods, or variables.
Compiler 10. Translates source code into machine code
Arithmetic Operations (5 points)
You have three variables listed below. Compute the results of using these values in the following expressions. The values of these variables will not change through the
computation of the following expressions (a will always have the value 42).
int a = 42;
int b = 7; double c = 4.0;
Write Your Own Expressions
You are given two variables that contain the two short sides of a right triangle. Assume
they have been initialized to some appropriate value.
double sideA;
double sideB;
Provide Java statement(s) to calculate and store the area of the triangle. Remember
that the area of a triangle = (1/2) * base * height and that this is a right triangle. Also
remember to declare the variable that you will store the area in. (5 points)
double area = 0.5 * sideA * sideB;
Provide Java statement(s) to calculate and store the length of the 3 rd side of the
triangle. Remember that the Pythagorean Theorem is a 2
. (5 points)
double sideC = Math.sqrt(sideA * sideA + sideB * sideB);
double sideC = Math.sqrt(Math.pow(sideA,2) + Math.pow(sideB,2));
c a
b
Writing a Class
You want to write a class to help a non-profit organizations keep track of the number of donations they have received. The NonProfitOrg class knows the name of the
organization, the total amount of money donated to the organization, and the number of
donors (both companies and individuals).
public class NonProfitOrg {
/**
private String orgName; private double money; private int numDonors;
public NonProfitOrg(String name, double money, int numD) { this.orgName = name; this.money = money; this.numDonors = numD; //Make sure to watch for this being used if the param //and instance variable have the same name }
public void acceptDonation( double money ) {
this.money += money; numDonors++;
//This method may have a parameter increasing the //number of donors as well โ anything that //looks right
public double averageDonations() {
double average = this.money / this.numDonors; return average;
//If parameters are used correctly throughout the //whole example class then no points taken off
While Loops (5 points)
Write a while loop to calculate 3 7 .
int power = 1 ;
int counter = 1 ;
while(counter <= 7 ) {
*power = 3 ;
counter++ ;
}
For Loops (5 points)
Write a for loop to calculate 3 7 .
int power = 1 ;
for(int i = 1 ; i <= 7 ; i++) {
*power = 3 ;
}
Nested Loops (10 points)
Write a nested loop that produces the following output:
for(int i = 1 ; i <= 4 ; i ++ ) {
for(int j = 1 ; j <= i ; j ++ ) {
System.out.print( j );
}
System.out.println(โโ);
}