Methods That Do And Do Not Return Any Value

Oct 8, 2014

Write the header for a method named send that has one parameter of type String, and does not return a value.Write the header for a method named average that has two parameters, both of type int, and returns an int value.

View Replies


ADVERTISEMENT

Multiple Methods Which Differs Only By Return Value

Jul 13, 2014

I have a class, which communicates with database via SQL queries. But one queries don't produce any ResultSet, while other queries do. Now I'm facing situation, where I have two methods, one which executes queries with return values and other which executes queries without return values:

public ResultSet executeQueryWithReturn(String query){
try{
return executeWithReturn(query);
}catch(Exception e){
e.printStackTrace();

[Code] .....

But I see few problems here: 1) Method can return null, (I read in book Clean Code, that people should avoid return null) Another issue - boolean value breaks the rule that method should do only one thing.

Maybe there is a better option? or maybe I should forget these two issues here for readability?

View Replies View Related

Servlets :: Some Methods Such As ServletRequest GetParameterValues Return A String Array

Jan 5, 2015

Some methods such as ServletRequest's getParameterValues return a String array whereas others (e.g. HttpServletRequest's getHeaders) return an Enumeration. Do these return types need to be learned parrot-fashion, or is there some sort of logic to it?

View Replies View Related

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

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

Calculator Only Return 0.0?

Sep 2, 2014

I am making a calculator in Java. However, when I press the "=" button on my calculator, it always returns 0.0, no matter what. I don't understand why? The code is below:

import javax.swing.*;//import the packages needed for gui
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.List;
import static java.lang.Math.*;
public class CalculatorCopy {

[Code] ....

View Replies View Related

Displaying A Return Value

Jan 18, 2014

I am working on a program to have a user input two positive integers and then finding the greatest common denominator between the two. My problem is, I don't know how to display the number (num2) that is returned after the method. Putting a simple System.out.println gives me the error "unreachable code".

import java.util.Scanner;
public class ****
{
public static void main(String[] args)

[code]....

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

Boolean Return Missing

Sep 25, 2014

So I'm trying to check if the new coordinates vs original coordinates are diagonal and 1 line further, and if there is a piece there(getNum()) if it's 2 lines further, so I'm trying to return a boolean value then.

so if it's the first if, it returns true, if it's the 2nd it returns true, then I say else for all other scenario's, and return false there, but my compiler says my method is missing a return statement.

public boolean check(int[] d) {
int x,y;
x = loc[0][0];
y = loc[0][1]; 
int sx = d[0];
int sy = d[1];

[Code] .....

Edit: used a local boolean and returned that after my if's.,

View Replies View Related

Return A String In While Loop

Nov 18, 2014

Here, I want to avoid using the line **return"";** and instead return all of the values I output with **System.out.println();**

I have tried using a **StringBuilder** method, but this outputs nothing when I run my test program. I am guessing that StringBuilder must work differently in a while loop, but I can't find how!

[URL] ....

public String toString(){
int topLine = size;
int topCurrentLine = size;
int bottomLine = 1;
int bottomCurrentLine = size;

[Code] ....

View Replies View Related

How To Return 2 Lowest Scores

Mar 19, 2015

If this method returns the lowest score, how do I return the 2 lowest scores.

For example If I input 1, 2, 3, 4, 5 this code will return 1.

How do i return the two lowest scores, for example if I input 1, 2, 3, 4, 5, I want to return 1, 2

public double getLowestScore() {
smallest = testScores[0];
double sum = 0.0;
double total = 0.0;
double Average = 0.0;

[Code] ....

View Replies View Related

Return Lowest Value From Array

Aug 12, 2014

I am trying to return the lowest value from the array. having trouble trying to capture the return value by placing it into the variable ..

int myLowest = getLowest(yourNumbers); and then printing out myLowest

import java.util.*;
public class numArrays
{
public static void main(String[] args){
int numbers;
int[]yourNumbers;

[code]....

View Replies View Related

Return Char To Main

Oct 14, 2014

public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int hours = getHours(kb);
char major = getMajor(kb);

[code]....

I'm trying to return a char c,o, or x if that is their "major code" that the scanner grabbed.

View Replies View Related

Return Statement In A Constructor

Jan 8, 2014

What is the difference between two statements:

Class1 class1 = new Class();
class1 = Class2.method1();

and

Class1 class1 = Class2.method1();

I have one more query on the same lines ... I always need to call the method1 of Class2 whenever i create a object of class1. So I wanted to go with the constructor in Class1. But the method1 in Class2 has a return statement. so is there any better way to do this other than constructors.

Sample code:

public int class Class2{
public static method1(){
return 2;
}
}
public class Class1{
public Class1(){
Class2.method1();
}
}

View Replies View Related

Distance Formula - Return Value NaN

Mar 17, 2015

I am working on this new project where we are using the great distance formula but every time I run my ending result is NaN. I was researching, and people say its because you divide by 0. I think I have my formula correct.

Java Code:

public class testingFormula {
public static void main(String[] args) {
double lat = 34.01858902;
double lon = -118.2817688;
double lat2 =33.94530869;
double lon2 = -118.3994904;

[Code] ....

View Replies View Related

Why Can't Get Return Value To Conversion Class

Oct 13, 2014

why i cant get the return value to my conversion class.

import java.util.*;
/*
*
*
public class MetricTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// declare a sentinal to exit the loop
String check = "Quit";

[code]....

View Replies View Related

How To Return Sum Of Multidimensional Array

Nov 21, 2014

what would the return be for adding two array's together,

public static int sum (int[][] a, int[][] b) {

int[] [] c = new int[a[0].length][b[0].length];
for (int i=0; i< a.length; i++){
for (int j=0; j<a[i].length; j++){
c[i][j] = a[i][j] + b[i][j];
}
}
return c[i][j];
}

My return is wrong...... I know, Any hints?

View Replies View Related

Keep Getting Zero Dollar Return When Run Main?

Feb 24, 2014

I keep getting a zero dollar return when I run my main. I am multiplying grossPay by hours but something is not catching.

public class employee {
private String name; //employees name
private int id; //employees id number

[Code].....

View Replies View Related

How To Return Value Certain As Number And Some As String

Nov 23, 2014

I've to return some value as a string and some as a int, how is this possible? Here's my code:

public class Card {
public void start(){
String [] suit = {"Spade","Club","Diamond","Heart"};
int [] number = {1,2,3,4,5,6,7,8,9,10,11,12,13};}
public String getColour(){
String [] suit = {"Spade","Club","Diamond","Heart"};

[Code] .....

So at the top, i've set it to return value as string because of the King Jack Queen and Ace, but i also have to return as numbers(int). And also i'm using a loop to read all the numbers, is there any other way?

View Replies View Related

How To Return Value (amount) In 10 Years

Mar 14, 2014

Here's the small project... but how can I return the value (amount) in 10 years ? The way I did, the program just doesnt return anything...

import java.util.Scanner;
public class FutureValue
{
public static void main (String []args) {
double amount = 0;
double apr = 0;
System.out.println("How much money would you like to invest?");

[Code] ....

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

Can Pass In Return Value In Parameter?

Jun 25, 2014

Can I pass in a return value in a parameter? I'm trying to do something like this:

Object o = new Object(anotherObject.method());

Where method returns an int. But when I try doing this it calls that method, rather than passing in the return value..

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

Return Value From Array To Main

Mar 11, 2014

I want to return values from arrays to the main and the problem is i cant use my variables from my constructor, I use new variables in my functions and i know this is no good, when I used the variables from the constructor in my function. I have a compilations errors,also i want to create un object in main and with this object i want to call the functions.

Java Code:

package javaapplication4;
import java.util.Scanner;
public class JavaApplication4 {
public static int[] MyInt;
public static double[] MyDouble;
public static String[] MyString;
public static char[] MyChar;

[Code] .....

The code is working when i run it and i have the right result in my screen but i know this is all wrong with the variables.

View Replies View Related







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