I was told to write a method that adds up the sequence of the formula (n/2n+1) eg. 1/3 + 2/5 + 3/7 etc. simple enough i suppose. my method is below
public static double Series(int n){
if (n==0)return 0;
else return (n/(n*2+1)) + Series(n - 1);
}
However for some reason or another it returns 0 for any number that is put in. I've written it dozens of different ways with no change and i feel like something fairly obvious is being missed on my part. I am honestly intrigued and interested as to why this is happening. i assume it has something to do with the way i put the actual formula in cause if i put anything else in like simply n the recursion would work as expected.
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;} } }
As one of the methods of my IntTree tree I have to implement a method that multiplies the level number with the sum of the nodes on the current level. So far I have this, and it doesn't work. What I am wondering is am I on the right track at all with the second return statement?
public int depthSum(){ return depthSum(overallRoot); } private int depthSum(IntTreeNode root) { if(root==null) return 0; int level = 0;
I'm trying to understand the concept behind this recursive method called rangeSum. This method sums a range of array elements with recursion. I tried summing the elements of 2 through 5, and I tried writing down what actually happens when java executes the program, but what I get when I try to figure it out by paper is different from what netbeans gives me. Here is a snapshot of my scratch work as well as my source code. Netbeans gives me "The sum of elements 2 through 5 is 18" when I try running it but it I get 12 when I do the recursion on paper. I know that the source code is correct because it's out of the book but what am I doing wrong when I try figuring it out by hand?
XML Code:
package recursivecall; import java.util.Scanner; /** * Author: <<Conrado Sanchez>> Date: Task: */ public class RecursiveCall {
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);
a)Write a method that recursively displays any given character the specified number of times on one line.For example, the call: displayRowOf Characters(,5);
Produce a line: *****Write another method that uses a for-loop to perform the same process.
B is something like this ? for (i=1; i<=n; i++) i= '*' * n; System.out.print(i);
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?
Alright, I don't understand how to link my compress method to my return statement method "getPText". Also in my compression I only want it to compress for 3 or more consecutive letters.
I need to write a method that will consume string representation of Object type and will return one object of this type. How to set return type for the method in this case?
Here is exmaple :
public <?> identifyType(String typeString){ if (typesString.matches("String")){ return new String(""); }else if (typeString.matches("Integer")){ return new Integer(0); } //....etc..}
I had a question about data structures. For the insert method of a linked list, the 'node' object is declared in the insert method. So doesn't the 'node' get destroyed as soon as the closing brace of the insert method is encountered? How do all the nodes continue to occupy memory?
I have this program I have to write(attached). I am having problems with what the structure will look like. The following what I have so far. The questions I have are in bold.
>get userInput of how many observations
>for(int i = 1; i <= userInput; i++) >for(int j = 1; j <= 1; j++) >use a switch(case) method to ask user to select an option(1,2,3) >example, user chooses option 1: >ask to input time >print displacement >ask user to stop application(Y/N) >IF "No" is selected --------->>
How do I continue with the loop if user decides to not quit? -- And do I need to put this in each 'case'?
The two last methods stumped me. The return type to each is "DateTime", according to JUnit complaints.I know that I can use the "this" keyword to reference to the object. But how do I get these two methods to return the correct result?
I read on how to use the continue statement, but I'm failing in how to use it properly, mostly because it's not working. I'm supposed to print out what numbers are showing up and how many times for each. Plus, I have to print out 'times' instead of 'time' if there's more than one of a certain number. Right now, it's printing out all the numbers including the ones that don't get inputted.
import java.util.Scanner; public class occurrence { public static void main(String[] args) {
//scanner/values Scanner input = new Scanner(System.in); int number = 101;
I'm trying to calculate a certain value, but it is returning 0 and I'm not sure why. This is a measurement to define the angle from the equator and the center of the sun
For my project, I am trying to return the method average so the program can calculate the average after grades are enter. I'm not sure what I am doing wrong and how I should improve it. My code is below. This is my error message: "Project.java:71: error: '.class' expected".
import java.util.Scanner; public class Project { public static void main(String[] args)
Im having trouble with my method return. I am new to using methods and cant seem to grasp the idea on the return part. I have to write a method that tells if a number is prime or not. This is what I have so far and it wont compile because it is saying "missing return statement } "..
import javax.swing.JOptionPane; public class IsPrimeMethod { public static void main(String []args) { String primeNum; int number; int i = 2;
public class School { public final int MIN_GRADE = 0; public final int MAX_GRADE = 100; private int [] grades; public School( int students ) { grades = new int[students];
[Code] ....
The output is :
The numbers in the array are: 1 2 3 4 6 7 8 9 10 13 14 15 The highest grade is: 11
For some reason it is not giving me the highest number in my array. Did I write the method wrong ? Is my array even passing through HighestGrade ?
I am getting these errors with this code, I can not figure why this error is occuring.
what is the issue with this code?
Java Code: package ABC; import java.util.ArrayList; import java.util.Scanner; public class GetInputFromUser { private ArrayList<String> name = new ArrayList<String>();