16. Part 1.
public class Exercise1
{
public static void main(String[] args)
{
System.out.println("Correct!");
}
}
17. Part 2. (Total: 17 items)
Class cell phone Object
brand
model
LCD size
weight
dimensions/size
talk time
battery standby time
Nokia
Nokia n70
176 x 208 pixels
125 grams
109 x 53 x 24 mm
3.5 hours
11 days
Send_Message Call
Take_Picture Record_Video
23. public class Exercise2
{
public static void mail(String[] args)
{
System.out.print("+++++++++");
System.out.println("+++++++++");
System.out;println("++ ++")'
System.out.println("++ ++");
System.out.println("+++++++++")>
System.oul.printtn("+++++++++");
}
}
24. System.out.println() System.out.print()
System.out.print()
System.out.println()
public class Exercise2
{
public static void main(String[] args)
{
System.out.println("+++++++++");
System.out.println("+++++++++");
System.out.println("++ ++");
System.out.println("++ ++");
System.out.println("+++++++++");
System.out.println("+++++++++");
}
}
public class Lesson2
{
public static void main(String[] args)
{
System.out.println("The quick brown fox jumped over
the");
System.out.println("lazy dog and ran through the
forest.");
System.out.println(" ");
System.out.println(" ___ /");
System.out.println(" ^ ^ ");
System.out.println(" o / ");
System.out.println("Fox");
}
}
32. public class Lesson3
{
public static void main(String[] args)
{
final double d = 10.8;
final boolean b = false;
final String s1 = "quick brown fox";
final String s2 = "slow yellow fox";
final int i = 4;
System.out.println(d);
System.out.println(b);
System.out.println(s1 + " and " + s2);
System.out.println(i);
System.out.println((int)d - i);
}
}
public class Exercise3
{
public static void main(String[] args)
{
final double d = 5.5;
final String s1 = "tekki";
final int i = 4;
System.out.println(d);
System.out.println(s1 + " is cool!");
System.out.println(i);
System.out.println((int)d - i);
}
}
39. public class Exercise4
{
public static void main(String[] args)
{
final int A = 1, B = 0;
System.out.print(A);
System.out.print(A);
System.out.print(A);
System.out.print(A);
System.out.print(A);
System.out.print(A);
System.out.println(A);
System.out.print(A);
System.out.print(" ");
System.out.println(A);
System.out.print(A);
System.out.print(" ");
System.out.print(B);
System.out.print(" ");
System.out.print(B);
System.out.print(" ");
System.out.println(A);
System.out.print(A);
System.out.print(" ");
System.out.println(A);
System.out.print(A);
System.out.print(A);
System.out.print(A);
System.out.print(A);
System.out.print(A);
System.out.print(A);
System.out.println(A);
}
}
40. Part 2.
final int Y = 10, Z = 20;
System.out.println(Z/Y); //to print out “2”
System.out.println(Z+Y); //to print out “30”
System.out.println(Z-Y-Y); //to print out “0”
System.out.println(Z>Y); //to print out “true”
final int Y = 10, Z = 20;
System.out.println(2); //to print out “2”
System.out.println(30); //to print out “30”
System.out.println(0); //to print out “0”
System.out.println(true); //to print out “true”
public class Lesson4
{
public static void main(String[] args)
{
final int A = 3, B = 10;
System.out.println(A+A+A+B); // can be different
System.out.println(A-B);// can be different
System.out.println(A*B);// can be different
System.out.println(B/(A/A));// can be different
System.out.println(B%A);
System.out.println(A>B);// can be different
System.out.println(A<B);// can be different
}
}
41.
42. Operator Description Example
= assigns values from right side operands to left side operands a=b
+= adds right operand to the left operand and assigns the result
to left
a+=b is same as a=a+b
-= subtracts the right operand from the left operand and assigns
the result to left operand
a-=b is same as a=a-b
*= multiplies the left operand by the right operand and assigns
the result to the left operand
a*=b is same as a=a*b
/= divides the left operand by the right operand and assigns the
result to the left operand
a/=b is same as a=a/b
%= calculates the modulus using two operands and assigns the
result to the left operand
a%=b is same as a=a%b
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< left shift
>> right shift
46. public class Exercise5
{
public static void main(String[] args){
int x = 0;
System.out.println("Value is: " + x);
if(x%2 == 0)
{
System.out.println("Even!");
} else {
System.out.println("Out!");
}
}
}
47. public class Lesson5
{
public static void main(String[] args)
{
final double NUMBER = 24.18;
int flag = 0;
System.out.print("Initial value of flag is: ");
System.out.println(flag);
System.out.print("Initial value of NUMBER is: ");
System.out.println(NUMBER);
if(NUMBER > 0)
flag = 1;
else
if(NUMBER < 0)
flag = 2;
else
flag = 0;
switch(flag)
{
case 1: System.out.println("NUMBER is POSITIVE.");
break;
case 2: System.out.println("NUMBER is NEGATIVE.");
break;
case 0: System.out.println("NUMBER is ZERO!");
break;
}
System.out.print("Final value of flag is: ");
System.out.println(flag);
System.out.print("Final value of NUMBER is: ");
System.out.println(NUMBER);
}
}
48. 1. if (a == 5) {
b += 3;
} else {
if (b > 4) {
c = 3;
}
2. if (a == 0) {
b = 1;
} else {
if (a == 1) {
b = 0;
} else {
b = -1;
}
}
3. if (a == 2) {
b = 10;
} else {
b = 35;
}
c = true;
1. switch (sides) {
case 3: polygon = "Triangle"; break;
case 4: polygon = "Quadrilateral"; break;
case 5: polygon = "Pentagon"; break;
case 6: polygon = "Hexagon"; break;
case 7: polygon = "Heptagon"; break;
case 8: polygon = "Octagon"; break;
case 9: polygon = "Nonagon"; break;
case 10: polygon = "Decagon"; break;
}
49. 2. switch (answer) {
case ‘A’:
case ‘a’:
points+=100;
break;
case ‘B’:
case ‘b’:
points+=200;
break;
case ‘C’:
case ‘c’:
points-=100;
break;
default:
points -= 200;
}
65. import java.io.*;
public class Exercise7
{
public static void main(String[] args)
{
BufferedReader dataIn=new BufferedReader(new
InputStreamReader(System.in));
int x = 0;
x = 10;
if(x%2==0)
System.out.println("Multiple of 2");
else
System.out.println("NOT a multiple of 2");
}
}
66. if(x%2==0)
System.out.println("Even number");
else
System.out.println("Odd number");
import java.io.*;
public class Lesson7
{
public static void main(String[] args)
{
BufferedReader dataIn=new BufferedReader(new
InputStreamReader(System.in));
int x = 0;
String Str_1;
System.out.print("Enter an integer value: ");
try
{
Str_1=dataIn.readLine();
x=Integer.parseInt(Str_1);
}
catch(Exception e)
{
System.out.println("Error reported");
}
if(x%5==0)
System.out.println("Number is a multiple of 5.");
else
System.out.println("Number is NOT a multiple of 5");
}
}
73. public class Exercise8
{
public static void main(String[] args){
AccessMe access = new AccessMe();
System.out.println(access.printme());
}
}
74. public class DRINKCONTAINER
{
private String BRAND, COLOR;
private final double CAPACITY = 10.0;
public double AMOUNTCONTAINED = 0.0;
public double getContents()
{
return AMOUNTCONTAINED;
}
public String getBrand()
{
return BRAND;
}
public String getColor()
{
return COLOR;
}
public void setBrand(String str)
{
BRAND = str;
}
public void setColor(String str)
{
COLOR = str;
}
public void throwContents()
{
AMOUNTCONTAINED = 0;
}
public void addContents(double x)
{
double temp;
temp = AMOUNTCONTAINED + x;
if(temp>=CAPACITY)
{
System.out.println("Maximum Capacity reached.");
AMOUNTCONTAINED = CAPACITY;
}
else
{
AMOUNTCONTAINED = temp;
}
}
}
BRAND and COLOR
as String
CAPACITY in liters
(final double) = 10.0
AMOUNTCONTAINE
D (a double that will
tell how many liters is
already inside a
DRINKCONTAINER
object) that should not
getContents() –
returns
AMOUNTCONTAIN
getColor() – returns
COLOR
setBrand(String str) –
sets the value of
BRAND equal to str
setColor(String str) –
sets the value of
COLOR equal to str
throwContent
s() – sets
AMOUNTCO
NTAINED to
0
addContents(double x) – adds
the argument passed to the
value of
AMOUNTCONTAINED. Again,
AMOUNTCONTAINED should
not exceed CAPACITY
getBrand() – returns
BRAND
75. public class Container_Controller {
public static void main(String[] args) {
DRINKCONTAINER dc= new DRINKCONTAINER();
System.out.println("Setting container color to
black."); dc.setColor("BLACK");
System.out.println("Setting brand to BRAND X.");
dc.setBrand("BRAND X");
System.out.println("Adding 9 liters of content..");
dc.addContents(9);
System.out.println("Adding 2 liters of content..");
dc.addContents(2);
System.out.println("Adding half liter of
content.."); dc.addContents(9);
System.out.println("CONTAINER DETAILS: ");
System.out.println("color: "+dc.getColor());
System.out.println("brand: "+dc.getBrand());
System.out.println("contents: "+dc.getContents() + "
liters");
System.out.println("Emptying container...");
dc.throwContents();
System.out.println("Container contents:
"+dc.getContents() + " liters");
}
}
dc.setColor("BLACK"); //
dc.setBrand("BRAND X"); //
dc.addContents(9); //