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?
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.
Here is my HashMap and a method for listing all the keys in it
HashMap<String, String> exampleOne = new HashMap<String, String>(); public void allKeys() { int i; i =0; for (String name: exampleOne.keySet())
[Code]....
Now I want to return all values that associated with one key. How do I do this? Or is it possible to other way round? I mean return All keys associated with a value?
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?
I am confused when it is proper to use an application with multiple entry points, or I guess an application with multiple interconnected modules. I have a network application (Netty) as well as a web application (spring). I can bundle them together, in effect tightly coupling them together, or I can modularize them to operate interdependently of each other while still working together to make the application whole.
Is there any specific reason for making an application a single entity vs multiple entities? Is it "desired" to have a self contained application (eg. One main method)?
I'm working on an assignment where I need to take radius and height from the user to use with the methods in class Cylinder. I have to use radius and height as input parameters in the methods that calculate: Base area, lateral area, total area, and volume. But when I use height and radius as input parameters then it just prints zeros for all calculated values. When I remove (double radius) from the method it works just fine. So my question is how can I get this to work with radius/height as input parameters? Or am I just misunderstanding will they still be input parameters even if I don't have it written as (double radius) in the method?
This current one is to calculate a planes holding pattern. I have to write a method to prompt user to enter speed in knots, then it converts it to km/hr. Another method to calc. pattern width using the speed, another method to calc. pattern length, than a main method which would call and print out the speed in knots, speed in km, pattern width and length.My current problem is on the second method. It works in that I can enter the values and it gives me the correct answers, however it's asking me to enter the speed twice, instead of just once. Anything I try just results in errors and won't compile.
import java.util.Scanner ; //main method public class TitleRemoved { public static void main(String[] args) { double airSpeedKm = airSpeedOts () ; System.out.println("That speed is " + airSpeedKm + " km/hr.") ;
[code].....
I want my code to not only work, but be organized and easily readable as well, so I want to avoid bad habits.
I'm trying to build a program that will output what will ultimately look like a simple mario level turned on its side. As part of my output I need the user to define what mario looks like. I do this using Scanner and save the input to String mario. When I try to use that variable in another method it gives me troubles.
import java.util.Scanner; public class Mario2 { public static void mario() { //user defines mario String mario = ">->O"; Scanner keys = new Scanner(System.in); System.out.println("What does mario look like?"); mario = keys.next(); System.out.println("Mario now looks like: " + mario);
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;} } }
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 have i am trying to implement tooltip through javascript, like when we click on an image link tooltip should be displayed and it should have close button/ close image to close that tooltip.like the same way i will have multiple images on page, when ever i click on the images all tooltips should be displayed on the page when ever i want to close that then only it should close through close button on tooltip.can we do it through java script or will go for jquery.
I'm very new to Java, and I am creating a program that takes multiple user input to create one face. I have a class for the eyes, nose, lips, and headshape. For some reason, my program is not drawing the graphics. ***for question purposes, I have only included my head shape class and my test class****
my "test" class:
import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class FaceTest { public static void main(String[] args) { String head = JOptionPane.showInputDialog("Would you like a circle, square, rectangle shaped head?: ");
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;} } }
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 {
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)
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
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.,
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;
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(); } }
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;
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";