Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

It is spring cleaning time on planet Galax. The Galaxers want to discard items t

ID: 3703182 • Letter: I

Question

It is spring cleaning time on planet Galax. The Galaxers want to discard items that begin with ! or end with @, for instance !?xq# and $?z2@. They own ten such items, but don’t know how to extract the names from their file (items.txt). “Here’s a program that will do that for you, Galaxers. It reads in the name of each item, stores the ones to be discarded in an array, and then prints the list on the screen….” import java.util.*; import java.io.*; public class SpringCleaning { public static void printList (String [ ] list) { } public static void main (String [ ] args) { } }

SECOND PROBLEM

”Here’s something that I think you’ll find out of this world, Galaxers: A method can call itself, a process known as recursion. Suppose we want to count the number of stars that appear in a Galaxian word: System.out.println(countTheStars(“**!?$a%f**#*”)); 5 System.out.println(countTheStars(“*!z3#?*”)); 2 The following recursive method will do just that: public static int countTheStars (String s) { }

Explanation / Answer

SpringCleaning.java

import java.util.*;

import java.io.*;

public class SpringCleaning {

public static void printList(String[] list) {

}

public static void main(String[] args) throws FileNotFoundException {

File file = new File("items.txt");

if(file.exists()) {

Scanner scan = new Scanner(file);

int n = 0;

while(scan.hasNext()) {

String item = scan.next();

if(item.charAt(0)=='!' ||item.charAt(item.length()-1)=='@') {

n++;

}

}

String list[] = new String[n];

Scanner read = new Scanner(file);

int i = 0;

while(read.hasNext()) {

String item = read.next();

if(item.charAt(0)=='!' ||item.charAt(item.length()-1)=='@') {

list[i]=item;

i++;

}

}

printList(list);

} else {

System.out.println("File items.txt does not exist");

}

}

}

Methods.java

public class Methods {

public static void main(String[] args) {

System.out.println(countTheStars("**!?$a%f**#*"));

}

public static int countTheStars (String s) {

int count = 0;

for(int i=0;i<s.length();i++) {

if(s.charAt(i)=='*') {

count++;

}

}

return count;

}

}

Output:

5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote