i need the answer for lap questions. i do not need the code. this in java progra
ID: 3734534 • Letter: I
Question
i need the answer for lap questions. i do not need the code. this in java program
Lab Report Questions:
Define a static method and give when is it most appropriate to use a static methods?
Is it possible for a static method to call a non-static method?
Objective:
Write a program that creates a helper class called StringHelper. This class has methods not already built into strings, but should be because they are super useful.
First create a class called StringHelper which has the following static methods
meshStrings: This method takes in two strings via parameters, meshes them together, and returns the meshed strings. Meshing alternates the each character in the first string with every character in the next string. If there are not enough characters to fully mesh then the rest will be appended to the end. For instance if the two strings were “harp” and “fiddle” the returned string will be “hfairdpdle”.
replaceVowelsWithOodle: This method takes in one string via parameter, and returns the string with every vowel (a,e,i,o,u) replaced by the phrase “oodle”. For instance if the string is “burrito” then the returned string would be “boodlerroodletoodle”. Also case does not matter.
weight: Much like length returns the number of characters in a string, the weight gives the weight in kilograms. This method takes in a string and returns a double value corresponding to its weight. Everyone knows that a word’s weight is determined by each vowel (a,e,i,o,u) counting as 2.5 kilograms and each consonant as 3.4 kilograms.
Finally create a class called StringHelperTester
This class DOES HAVE a main method
Test each method multiple times to determine if they work.
Example Dialog:
Welcome to the String Helper Tester
Meshing harp with fiddle
hfairdpdle
Replacing vowels with oodle in the word burrito
Boodlerroodletoodle
The weight of the word taco is
11.8
Lab Report Questions:
Define a static method and give when is it most appropriate to use a static methods?
Is it possible for a static method to call a non-static method?
Explanation / Answer
Please find the answers to the "Lab Report Questions" : -
Question # 1 : Define a static method and give when is it most appropriate to use a static methods?
Static methods in Java are class level methods, i.e., the method can be called using the class name only and no instance variable creation is required.
Satic methods can only access static variables and static methods.
Usage of static method :
Define a Static method when
1 ) an Utility method is required which which can be called by using the class Name itself.
2) The method can be used by many different types of classes
3) To access
For example, the below method " isValidString(String string)" is a static method which does not depend of any instance variable for invocation. It also does not hold any state.
STATIC METHOD EXAMPLE :
=======================
QUESTION # 2 : Is it possible for a static method to call a non-static method?
Answer : Yes, it is possible to call a non-static method from a static method by using a Instance variable reference of a class. Static method can not call a non-static method directly. Please see the example Below -
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.