LAB ACTIVITY
JAVA BASICS
WEEK 2 AND 3
PART A
1. What is the difference between C++ object oriented programming approach and
Java programming approach?
2. Which package is automatically imported by the Java system?
3. How to read data primitive using Java programming?
4. What does it means by the following statement "Java is a platform-independent
language"?
5. What is the Java Virtual Machine (JVM)?
6. If you have the source code for a Java program, and you want to run that program,
you will need both a compiler and an interpreter. What does the Java compiler do
and what does the Java interpreter do?
7. Java is case-sensitive. What does it mean by that statement?
8. The class name must be the same as the file name. (Yes/No)
9. What does the following instruction print?
System.out.println(“\nThis\nis\na\nJava\Programming”);
10. What is the difference between a variable and an object?
11. What are variables? (There are four different ideas associated with variables in Java.
Try to mention all four aspects in your answer. Hint: One of the aspects is the
variable's name.)
12. Which of the following is NOT a primitive data types? What is it’s data type?
int, boolean, float, char, String
13. Declare an integer variable value with initial value 10.
14. Declare a double constant FIXEDPAID with value 10.5.
15. Write a Java statement that initializes a String object with your name.
16. By assuming that String object had been initialized with this word Welcome, write
a Java statement that can print that word three times on the same line.
1
17. Which object that allow user to read primitive data type values and strings?
18. What is a class data value?
PART B
Write, compile and run the following program:
Exercise 1
public class TempConverter
{
public static void main(String[] args)
{
double fahrenheit = 100;
double celsius;
celsius = 0.56 * (fahrenheit - 32);
System.out.print(" degrees celsius is: ");
System.out.print(celsius);
System.out.println(" degrees fahrenheit:");
System.out.print(fahrenheit);
}
}
Exercise 2 (reading using JOption).
import javax.swing.JOptionPane;
public class Hello
{
//main method begins execution of Java application
public static void main( String args[] )
{
String firstNum; // first string entered by user
String secondNum; // second string entered by user
int num1; // first number to add
int num2; // second number to add
int sum; // sum of number1 and number2
//read in first number from user as a string
firstNum = JOptionPane.showInputDialog( "Enter first
integer" );
2
//read in second number from user as a string
secondNum = JOptionPane.showInputDialog( "Enter second
integer" );
//convert numbers from type String to type int
num1 = Integer.parseInt( firstNum );
num2 = Integer.parseInt( secondNum );
// add numbers
sum = num1 + num2;
//display result
JOptionPane.showMessageDialog( null, "The sum is: "
+ sum);
System.exit(0); //terminate application with window
}
} // end method main
Exercise 3
import java.util.*;
import java.text.*;
public class Circle
{
public static void main( String[] args )
{
final double PI = 3.14159;
double radius, area;
DecimalFormat df = new DecimalFormat("0.0");
Scanner console = new Scanner(System.in);
//Get input
System.out.print("Enter radius: ");
radius = console.nextDouble();
//Compute area and circumference
area = PI * radius * radius;
//Display the results
System.out.println("");
System.out.println("Given Radius: " + radius);
System.out.println("Area: " + df.format(area));
}
}
3
Exercise 4
String name;
int n;
boolean startWord;
name = "Nur Najla M. Hasif";
startWord = true;
for (n = 0; n < name.length(); n++)
{
if (startWord)
System.out.println(name.charAt(n));
if (name.charAt(n) == ' ')
startWord = true;
else
startWord = false;
}
Exercise 5
public static void main(String[] args)
{
int N;
N = 1;
while (N <= 32)
{
N = 2 * N;
System.out.println(N);
}
}