Circle your lecture section:
L0101 or L0102 or L0103
Name
Student Number
ECF Login
MARKS
| Question # | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | TOTAL |
| Value | 15 | 5 | 10 | 6 | 9 | 5 | 5 | 5 | 10 | 10 | 10 | 10 | 100 |
| Mark | |||||||||||||
if( a < b )
a = b;
else
b = a;
i and j are int variables whose
values are 8 and 5 respectively, show the output that would be produced
by the following statements. (Print one character in each space
provided.)
System.out.println("Sum is: " + i + j);
Sum is: 85
System.out.println("Quotient" + i/j);
Quotient1
System.out.println("\"mod\" " + i%j);
"mod" 3
class BadExample { public static
main (String[] args) { boolean done = false; while
done) { System.out.println(
Give a value - zero to stop");
value = Stdin.getInt(); if
value
0
done = true
else System.out.println(value + " " +
sqrt(value)
;
} }
rm Hello.class
nedit Hello.java
vi Hello.java
emacs Hello.java
pico Hello.java
javac Hello.java
java Hello
Machine code is the native language of a CPU, and is unique to each one.
Java bytecode is a special type of machinecode designed for the
Java Virtual Machine (JVM), whch is design to run on any processor.
There are times when you don't need the full range of values provided
by long and you need to save space (memory).
double y = 3.14159265;
float x = 2 * y;
The result of 2 * y is a double value, and this cannot be
assigned to a float in Java. You must do a type cast, since the
float variable may overflow when assigning a double
value to it.
Notice the order below -- we are using lazy evaluation to protect against the divide-by-zero error.
x >= 0 && z != 0 && x > y / z && z%2 == 0
class MyProg
{
public static void main(String [] args)
{
int a = 12, b = 3, c = 5 ;
System.out.println("start");
if (b>c)
for (int i = c; i <= b; i++)
if (c < a) System.out.println(c);
else
while (a > 0)
{
System.out.println(a--);
}
System.out.println("finish");
}
}
Indentation doesn't matter. Since the
else is associated with the most recent if,
this program prints:
start finish
int i, sum;
int n = Stdin.getInt();
sum = 0;
for( i = 1; i <= n; i = i * 2 ) {
sum = sum + i;
}
int i, sum;
int n = Stdin.getInt();
sum = 0;
i = 1;
while( i <= n ) {
sum = sum + i;
i = i * 2;
}
class Colinear
{
public static void main(String[] args)
{
int x1 = Stdin.getInt();
int y1 = Stdin.getInt();
int x2 = Stdin.getInt();
int y2 = Stdin.getInt();
int x3 = Stdin.getInt();
int y3 = Stdin.getInt();
// compare the slopes between points 1 to 3 versus 1 to 2
// if the slopes are equal, they must be colinear (because
// they share at least 1 as a common point).
// we must therefore test the following:
// slope12 == slope13
//
// which is just:
// y2-y1 y3-y1
// ----- == -----
// x2-x1 x3-x1
//
// or, after cross multiplying, the test becomes:
// (x3-x1) * (y2-y1) == (y3-y1) * (x2-x1)
//
// note this can be done with just integers
// (we'll ignore the chances of overflow)
int leftSide = (x3-x1) * (y2-y1);
int rightSide = (x2-x1) * (y3-y1);
if( leftSide == rightSide )
System.out.println("colinear");
else
System.out.println("not colinear");
}
}
*
*
*
*
*
class Stars
{
public static void main(String[] args)
{
int n;
do {
System.out.println("Please enter a positive integer");
n = Stdin.getInt();
} while( n <= 0 );
for( int row = 0; row < n; row++ ) {
for( int spaces=0; spaces<row; spaces++ ) {
System.out.print(" ");
}
System.out.println("*");
}
}
}
// This program ... asks the user to guess the largest prime
// number they can. It continues until a non-prime is guessed,
// after which it prints the largest prime entered.
//
//
class Z // new identifier: Z___LargestPrime______
{
public static void main(String[] args)
{
int A; // new identifier: A____userGuess________
boolean B; // new identifier: B____isGuessPrime_____
int C = 1; // new identifier: C____largestPrime_____
int D; // new identifier: D____divisor__________
do {
// Get a value >= 2 from the user
//
System.out.println("Guess a number:");
do {
A = Stdin.getInt();
} while( A < 2 );
// Test if the userGuess is prime, by first assuming it is prime.
// We will change our minds if we find a divisor.
B = true;
for( D=2; D < A; D++ )
if( A % D == 0 ) // we found a divisor
B = false; // it cannot be prime
// guess was successful, tell the user
//
if( B ) {
// remember the largest prime guessed
//
if( A > C ) {
System.out.println("yes! good guess, you're getting better!");
C = A;
} else {
System.out.println("yes! good guess, but not good enough!");
}
}
} while( B );
System.out.println("sorry, bad guess, but the best C was:");
System.out.println( C );
}
}
class RelativelyPrime
{
public static void main(String[] args)
{
int x = Stdin.getInt();
int y = Stdin.getInt();
int smaller;
if( x < y ) {
smaller = x;
} else {
smaller = y;
}
// test all possible divisors, see if they factor
// we only have to test up to the smaller number
boolean isRelativelyPrime = true;
for( int divisor=2; divisor <= smaller; divisor++ ) {
// done as soon as we find one divisor of both
if( x % divisor == 0 && y % divisor == 0 ) {
isRelativelyPrime = false;
break;
}
}
if( isRelativelyPrime )
System.out.println("They are relatively prime.");
else {
System.out.print ("They are not relatively prime.");
System.out.print ("They share the factor " )
System.out.println( divisor + "." );
}
}
}