Write a class StringLength.java with a class method stringLength() that takes a String…
- User
Write a class StringLength.java with a class method stringLength() that takes a String as parameter and returns the length of your string with the following return value: “Your string is characters long” (Hint: to be replaced with the actual length of the string). Use an instance method of the class String in your method. Run and test the method. without scanner and shortly please
- Assistant
Here is the code for the
StringLengthclass in Java:javapublic class StringLength { public static void main(String[] args) { String str = "Hello, world!"; System.out.println(stringLength(str)); } public static String stringLength(String str) { return "Your string is " + str.length() + " characters long"; } }Explanation:
- The
StringLengthclass has a main method which is the entry point of the program. - We define a string
stras "Hello, world!" for testing purposes. - We then call the
stringLengthmethod and pass thestrstring as a parameter. - The
stringLengthmethod uses thelength()method of theStringclass to get the length of the string, and returns a string with the length of the string concatenated with the rest of the sentence.
Output:
codeYour string is 13 characters long - The
- User
Write a class StringDigits.java with a class method onlyDigits() that checks whether a string contains only digits. Run and test the method (Hint: You can use methods of the classes String and Character). without scanner
- Assistant
Here is the code for the
StringDigitsclass in Java:javapublic class StringDigits { public static void main(String[] args) { String str = "12345"; System.out.println(onlyDigits(str)); // true String str2 = "12345abc"; System.out.println(onlyDigits(str2)); // false } public static boolean onlyDigits(String str) { for (int i = 0; i < str.length(); i++) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; } }Explanation:
- The
StringDigitsclass has a main method which is the entry point of the program. - We define two strings,
strandstr2, for testing purposes. - We then call the method and pass the strings as parameters.
- The
- User
In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, The sequence starts: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … Write a class Fibonacci.java with two methods fib1(long n) and fib2(long n) that take a number n and calculate the n-th fibonacci number. The first method should use an iterative approach, the second a recursive approach. Test it and for n = 50. What do you recognize? without scanner
- Assistant
Here is the code for the
Fibonacciclass in Java:javapublic class Fibonacci { public static void main(String[] args) { long n = 50; System.out.println(fib1(n)); // 12586269025 System.out.println(fib2(n)); // 12586269025 } public static long fib1(long n) { long[] fib = new long[(int) n+1]; fib[0] = 0; fib[1] = 1; for (int i = 2; i <= n; i++) { fib[i] = fib[i - 1] + fib[i - 2]; } return fib[(int) n]; } public static long fib2(long n) { if (n <= 1) { return n; } return fib2(n - 1) + fib2(n - 2); } } - User
public static void main(String[] args) {
codeint n = 10, firstTerm = 0, secondTerm = 1; System.out.println("Fibonacci Series till " + n + " terms:"); for (int i = 1; i <= n; ++i) { System.out.print(firstTerm + ", "); // compute the next term int nextTerm = firstTerm + secondTerm; firstTerm = secondTerm; secondTerm = nextTerm; }} } write this code with function methods
- Assistant
Here is the modified code using separate function methods to calculate and print the Fibonacci series:
java - User
In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, The sequence starts: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … Write a class Fibonacci.java with two methods shortly please without scanner
- Assistant
Here is the code for the
Fibonacciclass in Java:javapublic class Fibonacci { public static long fib1(long n) { if (n <= 1) { return n; } long[] fib = new long[2]; fib[0] = 0; fib[1] = 1; for (int i = 2; i <= n; i++) { long nextFib = fib[0] + fib[1]; fib[0] = fib[1]; fib[1] = nextFib; } return fib[1]; } public static long fib2(long n) { if (n <= 1) { return n; } return fib2(n - 1) + fib2(n - 2); } public static void main(String[] args) { long n = 10; System.out.println(fib1(n)); // 55 System.out.println(fib2(n)); // 55 } }