0% found this document useful (0 votes)
16 views7 pages

نسخة من Lab02- sol

The document provides solutions to 6 exercises involving ArrayLists in Java. The exercises cover storing user input in ArrayLists, checking for conditions on input to populate different ArrayLists, and methods to manipulate ArrayLists. Example code solutions are provided for each exercise.

Uploaded by

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

نسخة من Lab02- sol

The document provides solutions to 6 exercises involving ArrayLists in Java. The exercises cover storing user input in ArrayLists, checking for conditions on input to populate different ArrayLists, and methods to manipulate ArrayLists. Example code solutions are provided for each exercise.

Uploaded by

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

Lab #02

Al_Balqa’ Applied University


IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

LAB 02 Exercises with solutions

1- Write a program that lets the user type in names. Each


name that is entered should be stored in an ArrayList of
type String. When the word "quit" is entered then the
program should stop inputting names.

package arlist;
import java.util.*;
public class ArList {

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
String str=s.nextLine();
ArrayList <String> names= new ArrayList<String>();

while(str.equals("quit") != true)
{ names.add(str);
str=s.nextLine();
}

Rasha Moh'd Altarawneh Page 1 of 7


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

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++; }

}
}

Rasha Moh'd Altarawneh Page 2 of 7


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

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 {

public static void main(String[] args) {


Scanner s=new Scanner(System.in);
ArrayList <String>words = new ArrayList<>();
String word = s.nextLine();
words.add(word);
while(word.equals("quit")!=true)
{ word=s.nextLine();
String Last = words.get(words.size()-1);
if(word.charAt(0)==Last.charAt(Last.length()-1))
words.add(word);}

System.out.print(words);}}

Rasha Moh'd Altarawneh Page 3 of 7


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

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++; }

for (String aa : words)


System.out.println(aa);
}
}

Rasha Moh'd Altarawneh Page 4 of 7


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

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++; }

for(int j =0; j<marks.size() ; j++)


{
if (marks.get(j) >=50)
pass_marks.add(marks.get(j));
else
marks.set(j, 0);
}

for (int x :marks)


System.out.println(x);
System.out.println("================");

for (int y :pass_marks)


System.out.println(y); } }

Rasha Moh'd Altarawneh Page 5 of 7


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

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 {

public static void main(String[] args) {


ArrayList <String> list= new ArrayList<String>();
String str1;
for(int k = 0; k <= list.size() - 2; i += 2) {
str1 = list.get(i + 1);
list.set(i + 1, list.get(i));
list.set(i, str1);
}}

Rasha Moh'd Altarawneh Page 6 of 7


Lab #02
Al_Balqa’ Applied University
IT Collage
Java Programming Lab

Instructor: Rasha Moh'd Altarawneh

6- Write a method markLength4 that takes an ArrayList of Strings as a parameter


and that places a string of four asterisks "****" in front of every string of length 4. For
example, suppose that a variable called list contains the following values: {"this", "is",
"lots", "of", "fun", "for", "every", "Java", "programmer"} And you make the following call:
markLength4(list); then list should store the following values after the call: {"****", "this",
"is", "****", "lots", "of", "fun", "for", "every", "****", "Java", "programmer"} Notice that you
leave the original strings in the list, "this", "lots", "Java", but include the fourasterisk string
in front of each to mark it.

package arlist;
import java.util.*;
public class ArList {

public static void main(String[] args) {

ArrayList <String> strList= new ArrayList<String>();


strList.add("java");
strList.add("is");
strList.add("nice");
strList.add("programming");
strList.add("fun");
strList.add("to");
for(int k = 0; k < strList.size(); k++)
{ if(strList.get(k).length() == 4)
{ strList.add(k, "****");
K++;
}
}

Rasha Moh'd Altarawneh Page 7 of 7

You might also like