Set Method Return Type Determined Inside Method
Dec 25, 2014
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..}
View Replies
ADVERTISEMENT
Sep 7, 2014
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;}
}
}
View Replies
View Related
Apr 2, 2014
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);
[Code] ....
View Replies
View Related
May 21, 2015
I stumbled upon a method that im not sure how to handle (yet).Below the code:
public DateTime() {
this.day = day(milliseconds);
this.month = month(milliseconds);
this.year = year(milliseconds);
this.hour = hour(milliseconds);
this.minute = minute(milliseconds);
[code]...
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?
View Replies
View Related
Feb 7, 2015
why Java does not support return type in method overloading. I coded following and it compiles and runs without any errors.
class Ideone
{
public static void main (String[] args) {
sum(3,5);
[code]....
If Java did not support overloading based on return type, this program should not work, right?
View Replies
View Related
May 27, 2014
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?
View Replies
View Related
Oct 30, 2014
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.
import java.util.Scanner;
class RunLengthCode {
String pText;
String cText;
void setPText(String PText) {
pText = "";
}
[Code]...
View Replies
View Related
May 1, 2015
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
public static double declination(int dayOfYear) {
double declination = asin((sin(23.45)*sin((360/365)*(dayOfYear - 81))));
System.out.println("dec " + declination);
return declination;
}
View Replies
View Related
Jul 27, 2014
Here is the code:
import java.util.Scanner;
public class beersong
{
public static void main (String[] args)
{
int bottle;
int bottlenumber;
int bottlezero;
[Code] ....
And here is the problem:
return "nine";
^
error: return outside of methood
what do i do?
View Replies
View Related
Aug 1, 2014
Why in this code testSon class void display() method is called instead of testFather class void display()
class testFather
{
void display() {
System.out.println("This is testFather class");
}
testFather() //testFather constructor {
display();
[Code] .....
Output : This is testSon class /* why not This is testFather class*/
This is cons of testSon class
View Replies
View Related
Sep 3, 2014
if i call a class that implements an interface the method inside the interface will be triggered automatically by the compiler or happens only in the observer pattern? i keep simple to be surr the message came across, a typical example would be a listener on a button, or a collection that calls comparator)
View Replies
View Related
Dec 7, 2014
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)
[code]....
View Replies
View Related
Mar 11, 2015
This is my class code below and the Junit test that is being run.
My problem is that when I try to make an append to my list I have to return the actual list but I cant figure out where I am going wrong.
Java Code:
package structures;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ListImplementation<T> implements ListInterface<T> {
private Node<T> listHead, listTail;
private int size;
[Code] .....
View Replies
View Related
Apr 2, 2014
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;
[code]....
View Replies
View Related
Mar 2, 2015
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 ?
View Replies
View Related
Jan 14, 2014
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>();
[code]....
View Replies
View Related
Jan 1, 2015
class Base
{
public Base rtc() {
System.out.println("am in parent");
return new Base();
}
}
class Ret extends Base
[Code] ....
Output :
I am in child class but b1.rtc requires b2 to hold the return value y not Ret's object.
View Replies
View Related
Mar 5, 2015
For starters here is my code:
Java Code: class A {
int x=5;
}
class B extends A {
int x=6;
}
public class CovariantTest {
public A getObject() {
[Code] ....
And this is the output I get:
sub
5
I am unable to figure out how this is outputting 5 instead of 6. The getObject method of SubCovariantTest is obviously the one being called, and it returns a new B(). So why am I getting class A's x value? I thought since I was getting a B object returned that I would get B's x value.
View Replies
View Related
Jul 11, 2014
I'm having an issue with figuring out how to structure and call a method using a string prompt. My prof has given us specific methods to create, and the one I'm having issues with is blic static double getOperand(String prompt). What I have currently is this:
public static double getOperand(String operand1, String operand2){
Scanner input = new Scanner(System.in);
System.out.println("What is the first operand?");
operand1 = input.toString();
System.out.println("What is the second operand?");
operand2 = input.toString();
return operand1, operand2;
}
which is obviously not working. When I asked my prof he sent me this, "The String paramater in the getOperand() method is intended to have the prompt value that is passed to it. Within the method, the prompt is then displayed, what the prompt is asking for is input, and then returned to the method call.For example, if the method is public double getOperand(String prompt) within the method you would display the prompt, as in System. out. println(prompt), use the Scanner class to create an object, perhaps called input, and then use the input object to get the value asked for by the prompt. Once you have that value, which in this example would be a double, then return the value to the method that called it.
The call could be something like this: double value = getOperand("What is the first operand?");". But that only works a little. Clearly I'm missing something and no matter what I google, or how many times I read the chapter, I am just not getting this. Everything else up to this point has been easy. and I can create regular methods just fine, the menu method works, but I can't figure out this string method and all the rest build off this method.
View Replies
View Related
Nov 13, 2014
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.
View Replies
View Related
Oct 10, 2014
I tried to create a simple program. The main method is supposed to call out the secondary method to use Scanner in order to ask a person to type in a number, which will be assigned to int x. Then the secondary method supposed to return the int x to main method, which supposed to print it out. And for some reason I just can't do it. No matter what the main method refusing to accept the int x.
import java.util.Scanner;
public class Return{
static Scanner newNum= new Scanner(System.in);
public static void main(String[] args){
getInt();
System.out.println(x);
}
public static getInt( ) {
// TODO Auto-generated method stub
int x;
System.out.print("give a num");
x= newNum.nextInt();
return x;
}}
View Replies
View Related
Jan 21, 2014
I saw an example where an (inner)class is declared inside the main method, this is correct or not and why/when it's reasonable to use?so smth like this
public class myClass()
{
public static void myMethod(myInnerClass obj)
{
if (obj.method())
[code]....
View Replies
View Related
Mar 12, 2015
How do I create an instance of a class in a method?
I am a bit rusty whenever I think of instances. I always think of main method and objects when I see instance which gets me confused on what to do when I am not in a main method. The example:
I have a abstract class, School, and inside School I have some methods that must preform some action on an instance. For example, there is a move() method that must move the instance of School. Another method named, personOld(), which returns whether or not an instance of School surpassed some determined age.
How do I do this and create this instance?
View Replies
View Related
Nov 15, 2014
I've created a getMin method to return the smallest value in a queue. However, I'm having trouble calling the method in my main.
/**
* Main method.
*
* @param args
* the command line arguments; unused here
*/
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
Queue<Integer> testQueue = new Queue1L<Integer>();
[Code] .....
View Replies
View Related
Jul 7, 2014
I want to use this idea in a different context but for the sake of the argument lets suppose I have this method:
public int random number(int range)
{
Random rand = new Random();
int randomNumber = rand.nextInt(range);
.
.
.
return randomNumber;
}
How to avoid getting the same random number twice in a row?
View Replies
View Related
Apr 15, 2015
I'd like the code that would return an object's human readable name.ie. myObject.getObjectsName();Which would return the name of the object That is, the name of the object that I use as I code.In the code below, I'm trying to find the correct code to return the string,btnTTT_01..The results of the testing follow.
public static void main(String []args) {
tictactoe game ;
game = new tictactoe();
game.setVisible(true);
System.out.println ("main") ;
System.out.println ( "game.getName() is: " + game.getName() ) ;
System.out.println ( " " );
[code].....
View Replies
View Related