Java代写:COMPSCI121 Method

代写基础的Java作业,练习static和non-static method的使用。

Introductions

The goals of this class are to:

  1. Practice writing static and non-static methods.
  2. Learn the best practice of writing methods that do a single task.
  3. Understand method overloading.
  4. Understand how methods can call other methods to accomplish a task.

NOTE: This lab consists of the following activities

  • ACTIVITY 2 -Required: Your group needs to complete and individually submit in Moodle before the deadline.
  • ACTIVITY 3 - Optional: Once your group finishes the required activity, they can move to the challenging part, but the challenging part is not required for submission in Moodle.
  • ACTIVITY 4 - Review: For quick review of method functions.

Assume:

  1. You have installed the Java SDK and jGrasp on your machine. If not, make sure you install this software as soon as possible.
  2. You have created a COMPSCI121 folder and a Lab5 folder. You have downloaded and unzipped Lab5.zip file and placed the contents in “Lab5” folder.
  3. Opened “LAB 5 Methods” document.

Introduce yourselves to your group, and make sure you remember everyone’s names.

Before you start, complete the form below to assign a role to each member. If you have 3 people, combine Manager and Analyst. Upload the completed document in Moodle (preferably as a pdf).

Method Practice

Example

Compile and run the MethodTester class given to you. Study the isVowel method and test below, which is in the class.

1
2
3
4
5
6
7
8
9
10
11
public static boolean isVowel(char inputChar) {
  return (inputChar=='a' ||inputChar=='e' ||inputChar=='i'
           ||inputChar=='o' ||inputChar=='u'
           ||inputChar=='A' ||inputChar=='E' ||inputChar=='I'
           ||inputChar=='O' ||inputChar=='U');

 // test 1-isVowel
  boolean isVowel = isVowel('d');
  System.out.println(isVowel);
  isVowel = isVowel('A');
  System.out.println(isVowel);

Write a public static method and test in main method.

Write a replaceVowels method that takes a String parameter and returns a String with all vowels in the parameter String replaced by the asterisk character ‘*‘. For example, this call: replaceVowels(testStr) would return the String: “M*ss*ch*s*tts” if testStr was assigned to the String “Massachusetts”. When creating replaceVowels reuse the above isVowel method to check if a character is a vowel.

Write the replaceVowels method here by filling in the skeleton code.

1
2
3
4
5
6
7
8
9
// 2)replaceVowels
public static String replaceVowels(String inputStr) {
  String outputStr = "";
  for(int k = 0;k < inputStr.length(); k++)  {
   
// fill code here
  }
  return outputStr;
}

Now write a test for replaceVowels. It’s good practice to test your code as you develop it.
Write the call(s) for test 2 replaceVowels method here. Then you can test it in the main method of the MethodTester class, under // test 2-replaceVowels.

Write a public static method and test in main method.

Write another replaceVowels method that takes a String parameter and a char parameter. It returns a String with all vowels in the parameter String replaced by the char parameter that was passed in. For example, this call: replaceVowels(testStr, ‘G’) would return this String: “MGssGchGsGtts” if testStr was assigned to the String “Massachusetts”. Reuse the isVowel method given to you to check if a character is a vowel.
Write the replaceVowels method here. Note this is an example of method overloading (see Activity 4 for the explanation).

Now write a test for the replaceVowels. It’s good practice to test your code as you develop it.
Write the call(s) for test 3 replaceVowel method here. Then you can test it in the main method of the MethodTester class, under // test 3-replaceVowels.

Write a public static method and test in main method.

Write a replaceChar method that takes a String parameter and a char parameter. It returns a String with all characters in the parameter String replaced by the pound character ‘#’. For example, this call: replaceChar(testStr, ‘a’) would return this String: “M#ss#chusetts” if testStr was assigned to the String “Massachusetts”.
Write the replaceChar method here.

Now write a test for the replaceChar. It’s good practice to test your code as you develop it.
Write the call(s) for test 4 replaceChar method here. Then you can test it in the main method of the MethodTester class, under // test 4-replaceVowels.

Write a public static method and test in main method.

Write a replaceChar method that takes a String parameter and two char parameters. It returns a String with all characters in the parameter String that match the first char parameter replaced by the second char parameter that was passed in. For example, replaceChar(testStr, ‘s’,’%’) would return this String: “Ma%%achu%ett%” if testStr was assigned to the String “Massachusetts”. Note this is an example of method overloading (see Activity 4 for the explanation).

Now write a test for the replaceChar. It’s good practice to test your code as you develop it.
Write the call(s) for test 5 replaceChar method here. Then you can test it in the main method of the MethodTester class, under // test 5-replaceVowels.

Write a public static method and test in main method.

Write a printMiddleChar method that takes a String parameter and prints the middle character in that String; the method should not return anything. Assume the String passed in has length>0. For example, if printMiddleChar(“Massachusetts”) was called the method should print: “middle character: h”. If there is no middle character, as in printMiddleChar(“Abba”), the method prints: “no middle character”.
Hints:

  • You can find the middle index of a word by dividing the length of the word by 2.
  • You can use the remainder or modulus operator (%) to find if the length of the word is an odd number.

Write the printMiddleChar method here.

Now write a test for the printMiddleChar. It’s good practice to test your code as you develop it.
Write the call(s) for test 6 replaceChar method here. Then you can test it in the main method of the MethodTester class, under // test 6-replaceVowels.

WordsScramble

The class ScrambleMain given to you, is part of a two-class program that prompts a user to input a line of text and outputs the words in the text in a “scrambled” manner. Specifically, each word in the input text has its first and last characters swapped. For example, this input:

"The rain in Spain falls mainly in the plain". 
Would be scrambled: 
"ehT nair ni npaiS sallf yainlm ni eht nlaip". 

Note that words consisting of one character are printed as is.

The ScrambleMain class handles getting the user’s input from the keyboard and printing the scrambled text to the console.

TASK

Your task is to write the WordScramble class that does the scrambling. WordScramble has this public method:

1
public String scrambleLine(String inputStr)

The scrambleLine method takes a String parameter and returns a String that contains the same words as the input String with the first and last characters of each word swapped.
Assume the input has at least one word. Use the space character as a delimiter- the character that separates the words.

Write a private method scrambleWord to help perform the scrambleLine method. The scrambleWord method takes a String (a word) as a parameter and returns a String which is the word with the first and last characters swapped. You may write other private methods as you wish.

Remember to upload this file to Moodle!!

Methods - Review

What are objects and methods?

While Objects (made from class definitions) are the major organizing elements in Object-Oriented programming, methods are the “workhorses” of any programming language; they carry out one or more tasks. “Method” is the Object-Oriented term for “function”. A method should perform a single task. This makes the method easy to understand, write and troubleshoot. It also makes the code for the entire problem easier to read and understand as well as troubleshoot.

What are the parts of a methods?

A function has three major parts: a name- usually an action word (verb), a parameter list, and a return value- the output of the function.

What is method visibility?

To the three parts mentioned above, methods can be private or public. Since all method definitions are part of a class definition, their visibility to code outside their class definition is determined by whether they are defined as public or private. Public methods are part of a class interface as they determine how a class can be used. Private methods are never visible to outside code. They are only called by code inside the class.

What are static and non-static methods?

Methods can also be static. A static method cannot depend on any variables outside its scope. It can receive variables as parameters and return values just like any other method. Static methods are typically used by “utility” classes; classes that provide a set of related functions that perform common, useful tasks. One example is the Math class. Static code is compiled and available to all code in a program, so there is no need to create instances of static code. This is a more efficient use of computer resources.

What is method overloading?

An aspect of methods is that you may define more than one “version” of the same method. For example, you can have two different method definitions for a method named “replaceChar” as long as they have the same visibility (public or private), return type, name, but different parameters. This is called method overloading.

What is an algorithm?

The solution to most programming problems involves performing many tasks. The plan for solving such a problem is called an algorithm. An algorithm is a step by step procedure to solve a problem. In programming, it is a best practice to decompose the larger problem into its component tasks. These components are called subtasks, or subroutines. In Java, each subroutine is carried out by executing a method.