age:
yorAge:
new: reserved word
old:
dynamic:
static: reserved word
total amount: contains the space character
student_count:
$total:
height^2: contains the ^ character
yaş:
kuru:
for: reserved word
mor:
why?: contains the ? character
why_not:
if: reserved word
mif:
me2:
4you: starts with a digit
int i = 7;
double d = 2.0;
boolean flag = true;
System.out.println("Value of i "+i);
System.out.println("Value of d/i is \rd/i");
System.out.println(flag+"\rg");
System.out.println("This is \n\tthe last line\b\b?");
Value of i 7
d/iue of d/i is
grue
This is
the last li?e
See the sample code.
int a = 5; System.out.println(++a); System.out.println(a); a = 7; System.out.println(++a + a++); System.out.println(a);
6 6 16 9See the sample code.
/**
* @(#)Q_03_4.java
*
* @author H. Altay Güvenir
* @version 1.00 2013/2/26
*/
import java.util.Scanner;
public class Q_03_4 {
public static void main (String[] args) {
int totalAge = 0; // totalAge will store the total of the ages of the people
Scanner keyboard = new Scanner(System.in); // Declare the keyboard
System.out.print("Enter the age of the first person: ");
totalAge += keyboard.nextInt(); // Read an age and add to the totalAge
System.out.print("Enter the age of the second person: ");
totalAge += keyboard.nextInt(); // Read an age and add to the totalAge
System.out.print("Enter the age of the third person: ");
totalAge += keyboard.nextInt(); // Read an age and add to the totalAge
System.out.print("Enter the age of the fourth person: ");
totalAge += keyboard.nextInt(); // Read an age and add to the totalAge
// Now, totalAge contains the sum of the ages of the four people
// Display the average age
System.out.println ("Their average is "+ totalAge/4.0);
} // main
} // class Q_03_4
See the sample code.
public class Q_03_5 {
public static void main (String[] args) {
int count = 0;
int i = 4;
int j;
while (i > 0) {
j = i;
while (j-- > 0)
count ++;
i--;
} // outer while
// What is the output generated by the following statement?
System.out.println (count);
} // main
} // class Q_01_5
10See the sample code.
if-else statement using the switch statement.
if (harf == 'A')
System.out.println("Very Good");
else if (harf == 'B')
System.out.println("Good");
else if (harf == 'C')
System.out.println("OK");
else
System.out.println("Not Good");
switch (harf) {
case 'A' :
System.out.println ("Very Good");
break;
case 'B' :
System.out.println ("Good");
break;
case 'C' :
System.out.println ("OK");
break;
default:
System.out.println ("Not Good");
} // switch
See the sample code.
do-while statement, write and equivalent for statement.
do {
System.out.print("Enter your age: ");
age = kb.nextInt();
} while (age<0 || age>120);
for (age = -1; age < 0 || age > 120; ) {
System.out.print("Enter your age: ");
age = kb.nextInt();
}
See the sample code.
combination, that takes two int values,
called m and n, and returns the value of C(m,n). Note, assume m > n.
private static int factorial (int n) {
int result = 1;
for (int i=2; i<=n; i++)
result *= i;
return result;
} // factorial
public static int combination (int m, int n) {
return factorial(m) / factorial(n) / factorial(m-n);
} // combination
See the sample code.
sum, that takes a double array,
called d, as its formal parameter, and returns the sum of all values in the array, d.
public static double sum(double[] doubleArray) {
double sum = 0;
for (double d: doubleArray)
sum += d;
return sum;
} // sum
See the sample code.