Sie sind auf Seite 1von 3

http://www.cs.mcgill.

ca/~cs202/
1)
a)c >= '0' && c <= '9'
b)i % j == 0 || j % i == 0
c)s1.equals(s2)
d)Math.abs(d1-d2) <= .0001
2)
a)int 0
b)boolean false
c)String Fox
d)boolean false (note that it's always false since dividing by 5 never leaves yo
u with remainder of 7. If you noticed this you got a bonus point, if you proved
you knew what modulus meant you got full credit)
3)
a)
2,4,6,8,
b)
F,E,D,C,B,A
c)
xoF n*ilE
d)
*****
****
***
**
*
4)
a)
public String[] voteByRank(String[] names, int[] ranks) {
String[] orderedNames = new String[names.length];
for (int i=0; i < orderedNames.length; i++) {
orderedNames[ranks[i] - 1] = names[i];
}
return orderedNames;
}
b)
public String[] voteByIndex(String[] names, int[] indexes) {
String[] orderedNames = new String[names.length];
for (int i=0; i < orderedNames.length; i++) {
orderedNames[i] = names[indexes[i] - 1];
}
return orderedNames;
}
c)
public int[] convertFromIndexToRank(int[] indexes) {
int[] ranks = new int[indexes.length];
for (int i=0; i < ranks.length; i++) {
ranks[indexes[i] - 1] = i + 1;
}
return ranks;
}

5)
Point2D A = new Point2D(1.0,1.0);
Point2D B = new Point2D(2.5,1.5);
Point2D C = new Point2D(2.0, 2.0);
Vector2D AB = new Vector2D(A,B);
Vector2D AC = new Vector2D(A,C);
Vector2D AH = AC.projectOnto(AB);
Point2D H = A.moveBy(AH);
Vector2D HC = new Vector2D(H,C);
System.out.println("The area is " + HC.getLength() * AB.getLength() / 2);
6)
import java.util.Scanner;
public class FlintAnalyser {
public static void main(String[] args) {
System.out.println("Please enter the name of the file to be proc
essed");
Scanner keyboardReader = new Scanner(System.in);
String filename = keyboardReader.nextLine();
Scanner scan = new Scanner(new File(filename));
double height = scan.nextDouble();
int
int
int
int
int

blades = 0;
bladeletts = 0;
elongatedFlakes = 0;
wideFlakes = 0;
numFlints = 0;

while ( height != 0.0 ) {


double width = scan.nextDouble();
numFlints++;
if ( height > 2 * width ) {
blades++;
if ( height < 5 && width < 1.3) {
bladeletts++;
}
}
else if ( height > width) {
elongatedFlakes++;
}
else {
wideFlakes++;
}
height = scan.nextDouble();
}
System.out.println("Total number of flints processed: " + numFli
nts);

System.out.println(" - " + numBlades + " blades (" + blades * 10


0.0/ numFlints + " %)," + bladeletts + "(" + (bladeletts * 100.0) / (blades) + "
) of which are bladeletts.");
System.out.println(" - " + (elongatedFlakes + wideFlakes) + " fl
akes (" + ((elongatedFlakes + wideFlakes) * 100.0 / numFlints) + " %), of which
" + elongatedFlakes + "(" + (elongatedFlakes * 100.0 / (elongatedFlakes + wideF
lakes)) + "%) are elongated, and " + wideFlakes + "(" + (wideFlakes * 100.0 / (e
longatedFlakes + wideFlakes)) + "%) are wide");
}
}

7)
int numStars = 0;
int numOnes = 0;
for (int i =0; i < number.length(); i++) {
char digit = number.charAt(i);
if ( digit != '0' && digit != '1' && digit != '*') {
System.out.println("Invalid input");
return;
}
if ( digit == '*') {
numStars++;
if (numStars > 1) {
System.out.println("Corrupted beyond repair!");
return;
}
}
if (digit == '1') {
numOnes++;
}
}
if (numOnes % 2 == 0) {
String finalNumber = number.replace('*', '0');
System.out.println(finalNubmer);
}
else {
System.out.println(number.replace('*', '1'));
}
//Very short answers:
//String finalNumber = numOnes % 2 == 0 ? number.replace('*','0') : number.repla
ce('*', '1');
//String finalNumber = number.replace('*' , '0' + numOnes % 2);

Das könnte Ihnen auch gefallen