name:
private: reserved word
myOwn:
4sale: starts with a digit
new: reserved word
old:
new_old:
new&uded: contains & symbol
$sale:
free:
Değişken:
Variable:
_under:
*star: contains * symbol
hat^: contains ^ symbol
why?: contains ? symbol
comma,: contains , symbol
k76:
%5: contains % symbol
a/7: contains / symbol
int x=5;
float f = (float) 6.78;
System.out.println("x is "+ x+"\n\t\b"+f+" meters");
System.out.println("The results is shown above\ris reverse");
x is 5 6.78 meters is reverse is shown aboveSee the sample code.
/**
* @(#)Q_01_3.java
*
* @author H. Altay Güvenir
* @version 1.00 2013/2/25
*/
import java.util.Scanner;
public class Q_01_3 {
public static void main (String[] args) {
Scanner keyboard = new Scanner (System.in);
int s1; // Number of students in Section 1
System.out.print ("Enter the number of students in section 1: ");
s1 = keyboard.nextInt(); // read s1 from the keyboad
int s2; // Number of students in Section 2
System.out.print ("Enter the number of students in section 2: ");
s2 = keyboard.nextInt(); // read s2 from the keyboad
int s3; // Number of students in Section 3
System.out.print ("Enter the number of students in section 3: ");
s3 = keyboard.nextInt(); // read s3 from the keyboad
double average = (s1+s2+s3)/3.0; // compute the average
System.out.println ("Average number of students in a section is "+ average);
} // main
} // class Q_01_3
See the sample code.
/**
* @(#)Q_01_4.java
*
* @author H. Altay Güvenir
* @version 1.00 2013/3/6
*/
public class Q_01_4 {
enum ShirtSize {extraSmall, small, medium, large, extraLarge, extraExtraLarge};
public static void main (String[] args) {
ShirtSize narinSize, jaleSize, tosunSize; // shirt sizes of three friends
narinSize = ShirtSize.extraSmall; // Hale wares extra small size shirts
jaleSize = ShirtSize.small; // Jale wares small size shirts
tosunSize = ShirtSize.extraLarge; // Tosun wares large size shirts
System.out.println("Shirt size for Narin is "+ narinSize+".");
System.out.println("Shirt size for Jale is " + jaleSize+".");
System.out.println("Shirt size for Tosun is "+ tosunSize+".");
// Displaying the ordinal values of the variables of ShirtSize type
System.out.println ("Ordinal value of "+narinSize+" is "+narinSize.ordinal());
System.out.println ("Ordinal value of "+jaleSize+" is "+jaleSize.ordinal());
System.out.println ("Ordinal value of "+tosunSize+" is "+tosunSize.ordinal());
} // main
} // class Q_01_4
See the sample code.
public class Q_01_5 {
public static void main(String[] args) {
int x, y, z;
x = 5;
z = 0;
while (x-- > 0) {
y = x + 2;
while (y > 0)
System.out.println(y--%2==0?z++:z--);
} // while
System.out.println(z);
} // main
} // class Q_01_5
0 1 0 1 0 1 0 -1 0 -1 0 -1 0 -1 0 -1 -2 -1 -2 -1 -2See the sample code.
bottleList, which is an ArrayList of
WaterBottle objects, write a program segment that prints the
brandName of the WaterBottle object whose capacity
is the minimum.
WaterBottle minBottle = bottleList.get(0);
float minCap = minBottle.getCapacity();
// Using a for-each loop
for (WaterBottle wb: bottleList)
if (wb.getCapacity() < minCap) {
minBottle = wb;
minCap = minBottle.getCapacity();
}
System.out.println(minBottle.getBrandName());
See the sample code.
private static int factorial (int n) {
int result = 1;
for (int i=2; i<=n; i++)
result *= i;
return result;
} // factorial
public static double e (double x) {
double sum =0;
for (int k=0; k<20; k++)
sum += Math.pow(x, k) / factorial(k);
return sum;
} // e
See the sample code.
find, which takes an int array,
called ar, and another int value, called key.
It returns the index of key in ar.
If the key does not exist in ar, it returns -1.
public static int find (int[] ar, int key) {
for (int i=0; i < ar.length; i++)
if ( key== ar[i] )
return i;
return -1;
} // find
See the sample code.
double[][] as its parameter and returns
the sum of values in the array.
public static double sum2D (double[][] da) {
double sum = 0;
for (int r=0; r < da.length; r++)
for (int c=0; c<da[r].length; c++)
sum += da[r][c];
return sum;
} // sum2D
See the sample code.