Method Must Return Int Type - If Given Integer Is Strong Return A / If Not Return B

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


ADVERTISEMENT

How To Return Values In Object Return Type Function (method)

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

If Given Integer Is Even Return 1 / If Not Even Return 0

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 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;}
}
}

View Replies View Related

Why Java Does Not Support Return Type In Method Overloading

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

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;

[Code] .....

View Replies View Related

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 View Related

OOP A Method Being A Return Type In A Method?

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

Return Integer That Is The Number Of Strings That Starts With S

Apr 29, 2014

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 + +; 
        } 

View Replies View Related

If Statement Requires A Return Of Type Boolean

Dec 6, 2014

Im trying to do this

if( 2/2 ) {
system.out.println( "number is not event" );
} else {
system.out.println( "number is event" );
}

However if statement requires a return of type boolean. So im forced to this instead

if( (2/2) != 0 ) {
system.out.println( "number is not event" );
} else {
system.out.println( "number is event" );
}

Is there a way to achieve the former method, without typecasting, if you had to typecast how do you do it?

View Replies View Related

Find And Remove All Occurrences Of Given Integer And Return Amended Array

Jan 12, 2014

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 -

public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 4, 4, 5, 6, 7, 8, 8, 8, 8, 7, 7, 9};
int input = 8;
int newLen = nums.length;
for(int i=0; i<newLen; i++){
if(nums[i] == input){

[Code] ....

View Replies View Related

Count And Return Number Of Times Integer Occurs In Array

Nov 16, 2014

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++) {

[Code]...

View Replies View Related

Return Maximum And Minimum Numbers In Elements Of Integer Array

Oct 21, 2014

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)

View Replies View Related

Accept Array Of Non-negative Integers And Return Second Largest Integer

May 22, 2015

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++)

[Code] .....

View Replies View Related

Calculate Distance Between Two Points - All Numbers And Return Values Should Be Of Type Double

Jul 8, 2014

Write method distance to calculate the distance between two points (x1, y1) and (x2, y2). All numbers and return values should be of type double. Incorporate this method into an application that enables the user to enter the coordinates of the points.

Hints:

- The distance between two points can be calculated by taking the square root of

( x2 - x1 )2 + ( y2 - y1 )2

- Use Math class methods to compute the distance.

- Your output should appear as follows:

Type the end-of-file indicator to terminate

On UNIX/Linux/Mac OS X type <ctrl> d then press Enter

On Windows type <ctrl> z then press Enter

Or Enter X1: 1

Enter Y1: 1

Enter X2: 4

Enter Y2: 5

Distance is 5.000000

Type the end-of-file indicator to terminate

On UNIX/Linux/Mac OS X type <ctrl> d then press Enter

On Windows type <ctrl> z then press Enter

Or Enter X1: ^Z

View Replies View Related

Method Return 0 Value

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

Return Outside Method

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

Accept List Of Integers As Parameter And Return Number Of Times Most Frequently Occurring Integer

Nov 20, 2014

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.

If the list is empty, return 0.

View Replies View Related

Average Return Method

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

Why Cannot Get Append Method To Return Itself

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

Method Return Statement

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

Return Method Involving Arrays

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

Error When Trying To Return Array Via Method

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

Return Statement In Method Overriding

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

Covariant Return In Overridden Method

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

String Method To Return A Double

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

Why Recursive Method Continue To Return 0

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







Copyrights 2005-15 www.BigResource.com, All rights reserved