0% found this document useful (0 votes)
103 views

CS110T Lab02 PDF

This document provides instructions and exercises for a lab on Java basics. The objectives are to practice creating, compiling and running simple Java programs; using output statements like System.out.print and System.out.println; defining variables of different data types; and debugging code. The exercises include writing a program to print a name and message, identifying the output of code samples, debugging code with errors, and writing programs to print shapes and text.

Uploaded by

m
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

CS110T Lab02 PDF

This document provides instructions and exercises for a lab on Java basics. The objectives are to practice creating, compiling and running simple Java programs; using output statements like System.out.print and System.out.println; defining variables of different data types; and debugging code. The exercises include writing a program to print a name and message, identifying the output of code samples, debugging code with errors, and writing programs to print shapes and text.

Uploaded by

m
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CS110T: Programming Language1

Lab 02: Java basics I

Lab Objectives:
In this lab, the student will practice:
ü Creating, compiling and running a simple Java programs.
ü Using the output statements in Java: System.out.print and System.out.println
ü Defining variables of different data types
ü Debugging code.

L.Maram AlShablan
Lab Exercise 1: My First Program
Problem Description: Write a program that prints your name, your ID and “ I'm
studying Java “ message on a separate lines.

• The output should look something like the following:

Code:

Lab Exercise 2: Program Output


Problem Description: What is output of the following programs?

// Lab Exercise 2: Hello.java


public class Hello
{
// main method begins execution of Java application
public static void main( String args[] )
{
System.out.println(" I Like J\ta\tv\ta ");
System.out.print(" I’ll become \"a professional programmer\" \n");
} // end of the main method
} // end of the class
Output :

2
// Lab Exercise 2: Exercise2.java
// Defining variables of different data types

public class Exercise2


{
// main method begins execution of Java application
public static void main( String args[] )
{
int a=6;
double b=4.5;
char c='J';
boolean isBoolean=false;

System.out.println("a+b="+a+b);
System.out.println(c+" is for Java");
System.out.println("Pseudocode is a " +isBoolean+ " computer
code");
}
}

Output :

3
Lab Exercise 3: Debugging
Problem Description: The following program does not compile. Fix all the compilation
errors so that the program will compile successfully (9 errors).

// Lab Exercise 3: Java1.java


// Identifying and Correcting Errors

public Class
{
// main method begins execution of Java application
public static void main( string args[] );
{
system.out.println("Programming is","an exercise in learning how to
learn");
System.out.print(learning is never done without errors ")

} // end of Java1 class

Compilation errors:

4
Lab Exercise 4: Pascal Triangle
Problem Description: Write a Java program that prints the following Pascal Triangle.

5
Assignment Problems:
Problem Description:
1. Write a Java program that prints the following shape:

Problem Description:
2. What is the expected output of the following program:

public class Homework


{
public static void main (String args[])
{
System.out.println("Teachers can open the door, but you
must enter it yourself. \t\tChinese proverb");
System.out.println("Edition"+2+5);
}
}

Problem Description:
3. The following code segment does not compile. Fix all the compilation errors so that the
program will compile successfully.

// Find the Syntax errors and Correct them

public class Java Application


{
public static void main[string[] args]
{
Int a=2.3;
System.out.println("Just keep " , "practicing");
}

You might also like