نسخة من Lab02- sol
نسخة من Lab02- sol
package arlist;
import java.util.*;
public class ArList {
while(str.equals("quit") != true)
{ names.add(str);
str=s.nextLine();
}
2-Write a program that lets the user type a random number. Each even
number that is entered should be stored in an ArrayList of
type integer. When the number "-1" is entered then the
program should stop inputting numbers.
package arlist;
import java.util.*;
public class ArList {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int number=s.nextInt();
ArrayList <Integer> num= new ArrayList<Integer>();
int i = 0;
while(number != -1)
{ if (number % 2 == 0)
num.add(number);
number=s.nextInt();
i++; }
}
}
3- Write a game program that lets the user type a words. Each
word that is entered must begin by the last character of previous
word to be stored in an ArrayList of type String.
When the word "quit" is entered then the
program should stop inputting string.
Solution #1
import java.util.*;
public class Test {
System.out.print(words);}}
Solution #2
package arlist;
import java.util.*;
public class ArList {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String word1=s.nextLine();
ArrayList <String> words= new ArrayList<String>();
words.add(word1);
int i = 0;
while(word1.equals("quit")!=true)
{ word1=s.nextLine();
if (word1.startsWith(words.get(words.size()-1).substring(words.get(words.size()-1).length()-1)))
words.add(word1);
i++; }
4- Write a program that lets the user type a student's mark. Each mark
that is entered should be stored in an ArrayList of type integer. Then
check if the mark was greater than or equal 50 then stored in another
ArrayList , and set other marks to be zero.
package arlist;
import java.util.*;
public class ArList {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int m=s.nextInt();
ArrayList <Integer> marks= new ArrayList<Integer>();
ArrayList <Integer> pass_marks= new ArrayList<Integer>();
int i = 0;
while(m!=-1)
{ marks.add(m);
m=s.nextInt();
i++; }
5- Write a method switchPairs that switches the order of values in an ArrayList of Strings
in a pairwise fashion. Your method should switch the order of the first two values, then
switch the order of the next two, switch the order of the next two, and so on. For
example, if the list initially stores these values: {"four", "score", "and", "seven", "years",
"ago"} your method should switch the first pair, "four", "score", the second pair, "and",
"seven", and the third pair, "years", "ago", to yield this list: {"score", "four", "seven",
"and", "ago", "years"} If there are an odd number of values in the list, the final element
is not moved. For example, if the original list had been: {"to", "be", "or", "not", "to",
"be", "hamlet"} It would again switch pairs of values, but the final value, "hamlet"
would not be moved, yielding this list: {"be", "to", "not", "or", "be", "to", "hamlet"}
package arlist;
import java.util.*;
public class ArList {
package arlist;
import java.util.*;
public class ArList {