Simple Recursion Method - Accept Integer Parameter N And Return Sum Of First N Reciprocals
Nov 1, 2014
I am practicing some basic recursion and I was trying to solve this problem
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words:
sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n
For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should throw an IllegalArgumentException if passed a value less than 0.
This is my attempt to do it , however my output is always a 0.0 , and i do not understand why :
public static double sumTo(int n ){
if(n<0){
throw new IllegalArgumentException();
}
else if (n==0){
return 0.0;
Write a method maxOccurrences that accepts a list of integers as a parameter and returns the number of times the most frequently occurring integer (the “mode”) occurs in the list. Solve this problem using a map as auxiliary storage.
Write a function that accepts an array of non-negative integers and returns the second largest integer in the array.
Return -1 if there is no second largest.
The signature of the function is int f(int[ ] a)
Examples:
if the input array isreturn{1, 2, 3, 4}3{{4, 1, 2, 3}}3{1, 1, 2, 2}1{1, 1}-1{1}-1{}-1
In the signature what I understood is, I should write my function with the given signature,
The return type is "int"
method name is "f"
parameter is "a" right ?
Writing my doubts beside the particular line in the code
public static void main() // In the answer why they didn't use the class ?
In main method why they didn't use parameters ?(String[] args)
{ a1(new int[]{1, 2, 3, 4}); // what is "a1" here is it array name ? this line initializing the array ? a1(new int[]{4, 1, 2, 3}); a1(new int[]{1, 1, 2, 2}); a1(new int[]{1, 1}); a1(new int[]{1}); a1(new int[]{}); }
static int a1(int[] a) // what is "a" here parameter ? and "a1" is method name ? why they used the array name and method name same ?
{ int max1 = -1; int max2 = -1; for (int i=0; i<a.length; i++)
Now, i have an array of Client, so I would like to enter within this. Example:
public void addClient(Client c){ for (int i = 0; i<client.length ; i++) { // client is an array of Client object client[i] = c; // Enter a c in the array, but does not work! System.out.println("test "+clienti[i]); // print Client@15db9743 } }
I have used the println for check if worked insertion, but the result shows no
I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is strong return a, if it is notstrong return b. How would you write that in a Code?
I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.
mlong should return an int depending on the X.moth. at the moment my code looks like this:
// File1: public class date { public int day; public int month; public int year; }
// File 2: public class monthlength { public int mlong(date X) { int t; t = X.month; if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12) { return 31; } if(t == 4 || t == 6 || t == 9 || t == 11) {return 30;} } }
so i have this question where it wants me to create a recursion method that takes ONLY THE ARRAY as a parameter, and without using loops or static variables inside the method, and then the method returns the smallest value in that array. However, i tried making the simple if statements where i compare the first element of the array with the second element using the length of the array and decreasing it to get the next elements and compare it again by calling the recursion method, but the problem is when i call the method again, the length does not decrease, even if i store it in a variable, the variable will initialize itself again, and the length wont change.
Write a method named hopscotch that accepts an integer parameter for a number of "hops" and prints a hopscotch board of that many hops. A "hop" is defined as the split into two numbers and then back together again into one.For example, hopscotch(3); should print:
I wrote a couple classes and am trying a test driver but it is having an error I do not know how to solve.
Student Class:
public class Student{ private Course[] courseList; private static int numCourses; private final int maxCourses; public Student(int max){ maxCourses = max;
[Code] .....
Error: javac tester.java tester.java:6: error: cannot find symbol one = new Course(name); ^ symbol: variable name location: class tester 1 error
Same issue, just only one error as there is only one line. It seems like it does not accept my parameters as it cannot find symbol.
I forgot to put the "" in the brackets, it's been a month since I have looked at any java and made this simple mistake.
This program is supposed to accept an integer as an input and display the message that the number is even or odd. The main method calls a Boolean method. Write a method private static boolean iseven(int number and the message is printed from the Main method. This is what I have.
import java.util.Scanner; public class OddorEven { int number; public static void main(String[] args) { Scanner input = new Scanner(System.in);
[Code] ....
I know the message is not being printed from the Main method. I'm not sure how to do that.
I am attempting to write a program that accepts input of a positive integer, reports each digit and then the sum of all digits. This is what I have so far:
*/ package numbersum; import java.util.*; public class Week4NumberSum { static Scanner console = new Scanner(System.in); /** * @param args the command line arguments
[Code]...
It works if I enter 1234567891 (10 digits) Enter a positive integer: 1234567891 digit: 1 digit: 2 digit: 3 digit: 4 digit: 5 digit: 6 digit: 7 digit: 8 digit: 9 digit: 1
The sum of the digits is 46
But if I enter 11 digits, it fails:
Enter a positive integer: 123456789123 (12) Exception in thread "main" java.util.InputMismatchException: For input string: "123456789123" at java.util.Scanner.nextInt(Scanner.java:2123) at java.util.Scanner.nextInt(Scanner.java:2076) at week4numbersum.Week4NumberSum.main(Week4NumberSum. java:26)
java - Scanner error with nextInt() - Stack Overflow gives me some infor for InputMismatchException - which I find described as In order to deal with this exception you must verify that the input data of your application meet its specification. When this error is thrown, the format of the input data is incorrect and thus, you must fix it, in order for your application to proceed its execution.
I don't understand why the 10 digit integer is OK but the 11 or > digit integer is a mismatch.
I created this code, seems like it should be working properly and the code is right, but the output is not what it should be.The action gets a List<Integer> and should make it reversed.
As example: 1 2 3 4 5 ---> 5 4 3 2 1
The Nodes in the new list (lst2) should be in a reversed position.The code does compile with no errors.I do have Node<T> and List<T> classes
Java Code:
public class NodeReverse { public static void reverse (List<Integer> lst, Node<Integer> node, int counter, List<Integer> lst2, Node<Integer> pos) // Should make lst = lst2 while lst2 is the opposite to lst { node = node.getNext(); counter++; if(node.getNext()!=null) reverse(lst, node, counter, lst2, pos);
Write a java program to accept a string, float and an integer as command line arguments. String value should be displayed. Also the program should display the maximum value of two numbers. Use Integer.parseInt() and Float.parseFloat()
How do i take input values for TwoDPoint (which are objects) and return it back in numerical values also print them.
When i create an object in main method and pass values to my function of return type TwoDPoint,it gives error:- found int,int need TwoDPoiint,TwoDPoint.
// Here is what i tried to do:
Created class TwoDPoint that contains two fields x, y which are of type int. Defined another class TestTwoDPoint, where a main method is defined.In the main method created two TwoDPoint objects.
Then I modified the class TestTwoDPoint and add another function to it. This function takes two TwoDPoints as input and returns the TwoDPoint that is farthest from the point (0,0).
Then I added another function to TestTwoDPoint. This function takes two TwoDPoints as input and returns a new TwoDPoint whose x value is the sum of x values of the input TwoDPoint's and whose y value is the sum of the y values of the input TwoDPoint's.
class TwoDPoint { int x = 2; int y = 4; } class TestTwoDPoint { public static void main(String args[]) { TwoDPoint obj1 = new TwoDPoint(); System.out.println(obj1.x); System.out.println(obj1.y);
I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is even return 1, if it is not even return 0. How would you write that in a Code?
I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.
mlong should return an int depending on the X.moth.
At the moment my code looks like this:
// File1:
public class date { public int day; public int month; public int year; }
// File 2:
public class monthlength { public int mlong(date X) { int t; t = X.month; if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12) { return 31; } if(t == 4 || t == 6 || t == 9 || t == 11) {return 30;} } }
public class MyInteger { private int value; public MyInteger(int number){ value = number; System.out.println("Constructor created with value of " + value);
[code]....
I can't seem to get the value for integer1 and integer2 to pass with the setValue method. I get a runtime error stating the I need to have an int value for these two integers.
Write a Java code of the method startsWithCount that takes an array of strings words and a String S. The method should return an integer that is the number of strings that starts with S.
For example if:words = { "All", "Arab", "size", "Almond", "Allowed", "here"} and S= "All", then the method should return 2
PHP Code:
public class StringwithCount { public static void main (String[]args) { String strings[] = { "All", "Arab", "size", "Almond", "Allowed", "here"}; String output= ""; for ( int i = 0; i < words.length; i++) { if (words[i].startsWith("s")) c + +; }
We are supposed to create a MergeSort method without the using recursion. Most of the code is already completed, the only thing that I believe I need are two for loops (an inner and an outter) that will make calls to the merge method. I need implementing the sort method of the merge sort algorithm without recursion, where the length of the array is a power of 2. Keep merging adjacent regions whose size is a power of 2. For ex: lengths will be 1, 2, 4, 8, 16,.
public class MergeSorter { public static void sort(int[] a) { //for(int i = 1; i <= a.length; i++) the parameters for the for loop are wrong. { merge(0,0,1,a); merge(1,1,2,a); merge(2,2,3,a);
Question - Given an specific integer and an array of integers, find and remove all occurrences of the given integer and return an amended array. I solved it. Here is my solution -
I have problems getting the right number of times for each number of the array. Here is my code:
public static void main(String[] args) { int[] a = { 3, 13, 5, 9, 13, 6, 9, 13, 2, 3 }; int num = 3; int count = numbers(a, num); for (int i = 0; i < a.length; i++) {
1. Create a program that will return the maximum and minimum numbers in the elements of ONE-dimensional integer array. Save it as MaxMin_OneDim.java
2. Create a program that will return the maximum and minimum numbers in the elements of each row in a TWO-dimensional integer array. Save it as MaxMin_TwoDim.java
3. Write a program PrintPattern which prompt a user to enter a number and prints the following patterns using nested loops (assumed user entered number is 8 output is:)
1 .... 87654321
12 .... 7654321
123 .... 654321
1234 .... 54321
12345 .... 4321
123456 .... 321
1234567 .... 21
12345678 .... 1
(Without the dots, i just put them to give spaces)
i have to write a method, The method receives a parameter of two-dimensional array of integers. The method returns the number of the column which has the lowest sum of the integers.I'm allowed to use only recursion! no loops allowed!-of course i need to make a private method that will sum a column as a single array and then i have to do another private method that compares the column , but it doesn't really work .
I am trying to sum up the elements of an array. When I test my code the sum is always off by one. For example if I input: 20, 40,30 it gives me 89 instead of 90.
This is what I have so far:
public static void main(String[args]){ int size = kbd.nextInt(); int [] myArray = new int [size] //user inputs the elements of array
I'd like to know how to return a new array, I wrote in a method below the main method. I want to print the array but system.out.print doesn't work for arrays apparently. What structure i should use?