Download lab2_java-basics-i_revised

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS110D Programming Language I
Lab 2 : Java basics I
Computer Science Department
Fall 2017
Lab Objectives:
In this lab, the student will practice:
 Using System.out.print , System.out.println, and System.out.printf to
output text and characters to the command window.
 Compiling and executing Java applications.
 Writing and evaluating mathematical expressions
 Debugging code
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 :
Page|2
// 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 :
Page|3
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:
Page|4
// 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:
Page|5
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:
Page|6
Lab Exercise 4: Shape
Problem Description
Write java program that prints the following shape:
Code Skelton:
Page|7
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:
Page|8
Q2: Program 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:
Page|9
Related documents