Transcript lab8 s

CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014

Lab Exercise 1: program output • What is output by the following programs?

import java.util.Scanner; public class Lab_EX2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a number : "); int num = input.nextInt(); //the user will enter 3456 } } reverse(num); } public static void reverse (int n) { int reminder = 0; while ( n > 0 ) { reminder = n % 10 ; System.out.print(" " + reminder ); } n = n / 10 ;

Lab Exercise 1: program output public class Test

{ }

public static void

main (

String

args []){

System

.

out

.

println (

Math

.

max ( 12.123

, 12.456

));

System

.

out

.

println (

Math

.

max ( 23.12

, 23.0

)); }

 (Display patterns) Write a method to display a pattern as follows: 1 1 2 1 2 3 ...

1 2 3 …. n-1 n  The method header is

public static void displayPattern(int n)

} import java.util.Scanner; public class Lab10_EX3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a number for the pattern: "); int num = input.nextInt(); displayPattren(num); } public static void displayPattren(int n) { for (int i=1 ; i <= n ; i++) { for (int j=1 ; j <= i ; j++ System.out.print(j + " " ); System.out.println(); } }

Evaluation

Write a method printSquare that accepts character and side of square and prints a square of character. For example, the call: printSquare(a, 4); should produce the following output: aaaa aaaa aaaa aaaa Write a program to test your method.

Please enter character: M Please enter side of square: 3 MMM MMM MMM