M105, an Introduction to Object-Oriented Programming with JAVA
October 2009 offer
Name: S.MASH KID
University: AOU
Branch: Jeddah
Question 1: Primitive data types
The following program uses primitive data type short:
class ShortEg
{
public static void main (String[] args)
{
short num = 32;
System.out.println ("A short: " + num);
}
}
Create a file called ShortEg.java. Compile and run it by using BlueJ viewing the source code and the output screen shots.
2 out of 2 - Good,
The source code after compiling:
The output screen:
Num in this program is a variable; what is the main definition of variable?
2 out of 2 - Good,
I’ve got 4 different definitions:
- A variable is a data that is linked to value that has been stored ‘data’ in memory; it could be an expression that can be evaluated or change.
- It is used to store some data that will be retrieving later. (1)
- It is something that holds a value and store data. (2)
- A variable has three properties :( 3)
A memory location to store the value,
The type of data stored in the memory location, and
The name used to refer to the memory location.
In main memory how many bits num variable will hold?
2 out of 2 - Good,
In primitive data type: 1 byte = 8 bits
2 byte = (8 bits * 2 byte)
= (16 bits)
Therefore, the short data type is 16 bits
If we change the value of num to 35000 what will happen? Is there anything will change?
2 out of 2 - Good, First:
public class ShortEg02
{
public static void main (String[] args)
{
short num = 35000;
System.out.println ("A short: " + num);
}
}
Second:
Compile
The result of the compilation: I cannot get it successfully compiled
The error is “possible loss of precision”
Explanation:The ‘Short’ rang is from (-32768 to 32767). Therefore, the compilation is not possible because the variable 35000 is greater than the value of the ‘Short’ rang.The screen shot:
If we change the data type from short to int; is there a difference?
2 out of 2 - Good,
First:
public class ShortEg03
{
public static void main (String []args)
{
int num = 35000 ;
System.out.println("A int:" + num) ;
}
}
Second:
Compile
The result of the compilation: It successfully has been compiled
There is no error “class compiled – no syntax errors”
Explanation:The ‘int’ rang is from (-2147483648 to 2147483647). Therefore, the compilation is possible because the variable 35000 is smaller than the value of the ‘int’ rang.The screen shot ‘compile’:
The screen shot ‘run’:
Total of question one - 10 0ut of 10
Question 2: Variables and the Assignment Statements
Say that you are interested in the value of the quadratic:
3X2 -8X + 4
For several values of X. Write a program that has a double precision variable X. Assign a value to it. Write statement that computes a value for the quadratic and stores the result in another double precision variable. Finally write out the result, something like:
At X = 4.0 the value is 20.0
Run the program with several values for X (edit the program for each value of X) and examine the result. Use values with decimal points, large values, small values, negative values, and zero. The quadratic should evaluate to zero at X = 2.0 and at X = 2/3. Try these values for X. Are the results exactly correct?
Note: Provide a BlueJ screenshots for all options.
10 out of 10 - Good,
Part 01:
public class quadratic00
{
public static void main (String []args)
{
double x = 558557567 ;
double y = (3 * x * x) - (8*x) + 4 ;
System.out.println (" At x=" + x + " The Value is " + y) ;
}//main
}//class
Any value:x = 558557567
Decimal value:x = 4,0
Large value:x = 20600744,353
Small value:x = 0,0000035
Negative value:
x = - 20600744,353
Zero value:x = 0
Part 02:
Value 1:x = 2,0
The output ‘result’ after compiling and running the program…The value is = 0, 0. Therefore, yes, it’s correct.
The reason why the value is zero:
Because both numbers are ‘int’ “real numbers” and lest numerical over the most numerical is not an acceptable division. Therefore, the value became zero.
Value 2: x = 2/ 3The output ‘result’ after compiling and running the program…The value is = 4, 0. Therefore, no, it’s not correct.
The reason why the value ain’t zero:Because each number has a different type of numerical. It means that not both of them are real number. Therefore, the value cannot become zero.
The reason ‘why’ generally:Will it didn’t get the value zero because when I treat fractions in BlueJ program the program will not read 2\3 as the calculator would. It has to be written as 2.0\3.0 then when compiling the value it will become zero.
Total of question two - 10 0ut of 10
Question 3: Expressions and Arithmetic Operators
A) - Draw a flowchart for computing factorial N (N!)
Where N! = 1 * 2 * 3 * …… * N.
3 out of 5 - incorrect
B) - Write a program that:
Computes the sine of 0.5236 radians and saves it in a variable.
Computes the cosine of 0.5236 radians and saves it in another variable.
Computes the square of each those two values (use the variables), adds the two squares, and saves the result (in a third variable.)
Writes out the three variables.
The output statement should be something like:
System.out.println("sine: " + sinx + " cosine: " + cosx + " sum: " + sum );
Try a few other values b لاesides 0.5236.
4 out of 5 - Good
Variable 1 = 0.5236 \ The screen shot ‘compile’:
public class radians01
{
public static void main(String []args)
{
double cosx, sinx, sum;
cosx = Math.cos (0.5236);
sinx = Math.sin (0.5236);
cosx = Math.pow (cosx,2);
sinx = Math.pow (sinx,2);
sum = sinx + cosx;
System.out.println(" sine : " + sinx + " cosine : " + cosx + " * sum : " + sum);
}
}
The screen shot ‘run’:
Variable 2 = 0.04817 \ The screen shot ‘compile’:
public class radians02
{
public static void main(String []args)
{
double cosx, sinx, sum;
cosx = Math.cos (0.04817);
sinx = Math.sin (0.04817);
cosx = Math.pow (cosx,2);
sinx = Math.pow (sinx,2);
sum = sinx + cosx;
System.out.println(" sine : " + sinx + " cosine : " + cosx + " * sum : " + sum);
}
}
The screen shot ‘run’:
Variable 3 = 0.034568 \ The screen shot ‘compile’:
public class radians03
{
public static void main(String []args)
{
double cosx, sinx, sum;
cosx = Math.cos (0.034568);
sinx = Math.sin (0.034568);
cosx = Math.pow (cosx,2);
sinx = Math.pow (sinx,2);
sum = sinx + cosx;
System.out.println(" sine : " + sinx + " cosine : " + cosx + " * sum : " + sum);
}
}
The screen shot ‘run’:
Variable 4 = 35 \ The screen shot ‘compile’:
public class radians04
{
public static void main(String []args)
{
double cosx, sinx, sum;
cosx = Math.cos (35);
sinx = Math.sin (35);
cosx = Math.pow (cosx,2);
sinx = Math.pow (sinx,2);
sum = sinx + cosx;
System.out.println(" sine : " + sinx + " cosine : " + cosx + " * sum : " + sum);
}
}
The screen shot ‘run’:
C) - Translate the following scenario to a Java Program:
We assume that an array a of size n and a key x are given.
We are supposed to find an i satisfying the above specification.
For i = 0,1,...,n-1,
if a[i] is equal to x
then we have found a suitable i and hence we stop.
If we reach this point
then x is not in a and hence we terminate with i = -1.
8 out of 10 – Good-Scenario
import javax.swing.*;
public class Translate01
{
public static void main (String[] args)
{
int [] a = {3,4,5,6,7,1,2,3,8,5} ;
int x = 5 ;
int y = 1 ;
String b ;
b = JOptionPane.showInputDialog(null, " Pleas Enter X Value ") ;
x = Integer.parseInt(b);
for (int i = 0; i': case '!': case '?':
np++;
break;
case 'a': case 'A': case 'e': case 'E': case 'o':
case 'O': case 'i': case 'I': case 'u': case 'U':
nv++;
break;
case 'b': case 'B': case 'c': case 'C': case 'd':
case 'D': case 'f': case 'F': case 'g': case 'G':
case 'h': case 'H': case 'j': case 'J': case 'k':
case 'K': case 'l': case 'L': case 'm': case 'M':
case 'n': case 'N': case 'p': case 'P': case 'q':
case 'Q': case 'r': case 'R': case 's': case 'S':
case 't': case 'T': case 'v': case 'V': case 'w':
case 'W': case 'x': case 'X': case 'y': case 'Y':
case 'z': case 'Z':
nc++;
break;
}
}
System.out.println(" Number Of constants :"+ nc );
System.out.println(" Number Of vowels :"+ nv );
System.out.println(" Number Of punctuation characters :"+ np );
System.out.println(" Number Of spaces :"+ ns );
}
}
The Statement:My Love for JAVA is gone!. It’s so hard to people that doesn’t have a programmer mind<<