Lab 1
What is wrong with the following program statement? How can it be fixed?
System.out.println ("To be or not to be, that
is the question.");
The string to be printed is not all on one line. The problem can be fixed by using the string concatenation operator (+) or by using a print statement for part of the string and a println statement for the remainder of the string.
What output is produced by the following code fragment?
String m1, m2, m3;
m1 = "Quest for the Holy Grail";
m2 = m1.toLowerCase();
m3 = m1 + " " + m2;
System.out.println (m3.replace('h', 'z'));
The output produced is:
Quest for tze Holy Grail quest for tze zoly grail
The original string is concatenated with a lowercase version of itself, then all lowercase ‘h’ characters are replaced with ‘z’.
For each of the following pairs, which represents a class and which represents an object of that class?
Superhero, Superman
Justin, Person
Rover, Pet
Magazine, Time
Christmas, Holiday
Class: Superhero, Object: Superman
Class: Person, Object: Justin
Class: Pet, Object: Rover
Class: Magazine, Object: Time
Class: Holiday, Object: Christmas
List some attributes and operations that might be defined for a class called PictureFrame that represents a picture frame.
Attributes could include: height, width, empty (boolean)
Operations could include: getHeight, setHeight, getWidth, setWidth, isEmpty, fill
Write a short Java function, isMultiple, that takes two long values, n and m, and returns true if and only if n is a multiple of m, that is, n=mi for some integer i.
The modulus operator could be useful here.
public boolean (name of the function)(parameter, parameter) {
(use if statement)
}
public boolean isMultiple(long n, long m){
if (n%m == 0)
Return true;
else
return false;
}
Write a short Java function that takes an array of int values and determines if all the numbers are different from each other ( that is, they are distinct).
The simple solution just checks each number against every other one.
public boolean (name of the function)( parameter array of integers) {
(use for loop and method length-1)
(use for loop and method lenght)
(use if statement and compare array [i] and [j])
}
public boolean allDistinct(int[] ints){
for (int i=0; i<ints.length-1; i++)
for (int j=i+1; j<ints.lenght; j++)
if (ints[i] == ints[j]) return false;
return true;
}
LAB 1 WHAT IS WRONG WITH THE FOLLOWING
MALCOLM GLADWELL GOT US WRONG OUR RESEARCH WAS KEY
MUTATION ACTIVITY WHAT CAN HAPPEN WHEN THINGS GO WRONG?
Tags: following program, the following, following, wrong