0% found this document useful (0 votes)
68 views11 pages

Java Lab Exercises for Students

This document outlines 5 questions for a Java programming lab exercise. It provides the questions, explanations, and sample code solutions. The questions involve writing methods to find the maximum of 3 numbers, check if two arrays are equal, perform arithmetic and display the result, reverse a string, and horizontally flip an image. The document is for the CS175: Programming in Java practical course taught by Dr. Juma Lungo at UDSM.

Uploaded by

Pasiano Nemes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views11 pages

Java Lab Exercises for Students

This document outlines 5 questions for a Java programming lab exercise. It provides the questions, explanations, and sample code solutions. The questions involve writing methods to find the maximum of 3 numbers, check if two arrays are equal, perform arithmetic and display the result, reverse a string, and horizontally flip an image. The document is for the CS175: Programming in Java practical course taught by Dr. Juma Lungo at UDSM.

Uploaded by

Pasiano Nemes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

CS175: Programming in Java

Practical

Dr. Juma Lungo


[email protected]
Lab Exercises

2
Question One

 Write a static method max3() that takes


three int arguments and returns the value of
the largest one.

3
Question Two

 Write a static method eq() that takes two int


arrays as arguments and returns true if the
arrays have the same length and all
corresponding pairs of elements are equal,
and false otherwise.

4
Question Two - Sln
public class ArrayEquals {
// return true if two integer arrays have same length and all
// corresponding pairs of integers are equal
public static boolean eq(int[] a, int[] b) {
// same length?
if (a.length != b.length) return false;
// check each corresponding pair
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) return false;
}
// all elements must be equal
return true;
}
5
Question Two - Sln
// test client
public static void main(String[] args) {
int[] a = { 3, 1, 4, 1, 5 };
int[] b = { 3, 1, 4, 1 };
int[] c = { 3, 1, 4, 1, 5 };
int[] d = { 2, 7, 1, 8, 2 };

StdOut.println(eq(a, a));
StdOut.println(eq(a, b));
StdOut.println(eq(a, c));
StdOut.println(eq(a, d));
}
} 6
Question Three
 Write a java program to display the value of
x on the computer screen where
x = 46/6 + 5 % 7

UDSM
Question Four

 Write a function reverse() that takes a string


as an argument and returns a string that
contains the same sequence of characters
as the argument string but in reverse order.

UDSM
Question Four - Soln
public class ReverseString {
public static String reverse(String s) {
int n = s.length();
char[] a = s.toCharArray();
for (int i = 0; i < n/2; i++) {
char temp = a[i];
a[i] = a[n-i-1];
a[n-i-1] = temp;
}
return new String(a);
}
public static void main(String[] args) {
StdOut.println(reverse(args[0]));
}
}
UDSM
Question Five

 Write a program FlipX.java that takes the


name of an image file as a command-line
argument and flips the image horizontally.

UDSM
Question Five - soln

 import java.awt.Color; public class FlipX


{ public static void main(String[] args)
{ Picture pic = new Picture(args[0]); int width
= pic.width(); int height = pic.height();
pic.show(); for (int y = 0; y < height; y++)
{ for (int x = 0; x < width / 2; x++) { Color c1
= pic.get(x, y); Color c2 = pic.get(width - x -
1, y); pic.set(x, y, c2); pic.set(width - x - 1, y,
c1); } } pic.show(); } }
UDSM

You might also like