Download presentation
Presentation is loading. Please wait.
1
CS110D Programming Language I
Lab 2 : Java basics I Computer Science Department Fall 2017
2
Lab Exercise 1: program output
Lab Objectives: In this lab, the student will practice: Using System.out.print , System.out.println, output text and characters to the command window. Compiling and executing Java applications. Writing and evaluating mathematical expressions Debugging code and System.out.printf to Lab Exercise 1: program output Problem Description What is output by the following programs? // Lab Exercise 1: hello.java // Printing with multiple statements public class Hello { // main method begins execution of Java application public static void main( String args[] ) System.out.print("HELLO WORLD, "); System.out.println("I am a new programmer :)"); System.out.println("\"Programming is fun\""); System.out.printf("\"%s%s\"","Programming is ","fun"); } // end method main } // end class hello Output :
3
Output : // Lab Exercise 1: Math.java
// Computing and displaying the result of different expressions public class Math { // main method begins execution of Java application public static void main( String args[] ) System.out.print("((2-2) × 1×3) - 3/1.0= "); System.out.println(((2-2)*1*3)-3/1.0); System.out.println("4.0*1="+4.0*1); System.out.println("4.0+1="+4.0+1); System.out.println("4.0+1="+(4.0+1)); } // end main } // end class Math Output :
4
Lab Exercise 2: debugging
Problem Description The following code segments do not compile. Fix all the compilation errors so that the program will compile successfully. (4 errors in every code) // Lab Exercise 2: information.java // Printing with multiple statements public class information { // main method begins execution of Java application public static void main( String args[] ); System.out.print("My name is Ahmad); System.out.print(); }// end information class Correct code:
5
Correct code: // Lab Exercise 2: hello.java
// Printing with a printing statement public hello { // main method begins execution of Java application public static void main( String args[] ) system.out.println("HELLO everybody!"); }// end hello class Correct code:
6
Lab Exercise 3: calculate squares and cubes
Problem Description Write an application that calculates and prints the squares and cubes of the integers from 0 to 5. Sample output Code Skelton:
7
Lab Exercise 4: Shape Problem Description Code Skelton:
Write java program that prints the following shape: Code Skelton:
8
Assignment Problem(s)
Q1: More shape 1. Write a program that it prints the shape. The should have a side containing 8 asterisks. Code Skelton:
9
Q2: Program output Output:
What is the expected output of the following program: // Lab.2 Q.2: Homework.java // Program that prints the result of remainder operators public class Homework2 { public static void main (String args[]) System.out.print("The remainder of 40%7% 8="); System.out.println(40%7%8); } // end main } // end Homework2 Output:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.