Using Methods Within Methods
Jan 10, 2014
I have written two methods called "contains" and "overlaps". The method "contains" is to detemine whether a point (x, y coordinate) is within the surface area of a square object. The location of the square objects is determined by the location of the upper left corner of the square.The method "overlaps" is to determine whether two square objects overlap each other.
I have written these as two separate methods. However I want to change the method "overlaps", so that it uses the method "contains" within it. I.e. using a method within a method. Thereby hopefully making the "overlaps" method a bit more clear and easy to read.
Java Code:
/** Returns true of the point with the coordinates x,y, are within the square. */
public boolean contains(int x, int y){
int sx = location.getX(); //"location" refers to a square object
int sy = location.getY(); // getX() and getY() are to find it's coordinates
// "side" is the side length of the square
if (x >= sx && x <= (sx + side) && y >= sy && y <= (sy + side)){
[code]....
View Replies
ADVERTISEMENT
Jun 22, 2014
Below is the main class of a project ive been working on, the goal is to start a countdown specified by the user. When the countdown reaches zero the base drops in the song that is being played. (Its not done yet) The main problem that arises is the fact that my song plays, and AFTER that, the timer starts.
Output:
Please input countdown in HH:mm:ss format.
00:00:41
Start?
Yes
The name of of the song is: Skrillex & Damian "Jr Gong" Marley - "Make It Bun Dem"
The time of base drop is: 00:00:41 //Song starts here
//Song is done
//Then timer starts
00:00:41
00:00:40
00:00:39
[code].....
View Replies
View Related
Feb 5, 2014
I have following code. In this code CSClient is an interface. All methods of CSClient are implementaed in CSClientImpl class. Do I not need CS Client Impl imported in this code ?
How can I call getBranch() of CSClient, which is not implemented in CSClient as " this. getCsClient(). get Branch (new CSVPath(vpath), true);" ? This code works fine without any error in eclipse.
How can a method getBranch(), which is implemented in CSClientImpl class be used in this code without importing CSClientImpl ?
package com.rbc.teamsite.client;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
[code]....
View Replies
View Related
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
View Related
Dec 1, 2014
I have seen these two methods of using ItemListener. I am curios which is better and why. What are the differences?
ItemListener a;
Choice ce = new Choice();
ce.addItemListener(a);
[code]....
View Replies
View Related
Apr 14, 2015
I have had to create a text analyser. I have created the program but it is all within the main method. The specification states that I have to have at least two methods within my Program.
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Analyser {
public static void main(String[] args) throws FileNotFoundException,
UnsupportedEncodingException {
[code]....
View Replies
View Related
May 1, 2015
Java Code:
package methods;
import java.util.*;
import javax.swing.*;
public class Methods {
public static void main(String[] args) {
// TODO Auto-generated method stub
char choicee2;
[Code] .....
View Replies
View Related
Aug 22, 2014
I'm working on a side project, which will eventually hopefully be a Pokedex, and I've just been going to it at the end of every chapter and using the stuff I've learned to work on it.So I just read chapter 3, which is all about variables and teaches how to use arrays.
my question is, does an array have to be declared inside a method? Because I'm trying to create an array inside a class without any methods and without the main, and I continuously get errors. Here's a quick working of my code that won't compile.
class blah {
blah a[] = new blah[7];
a[0] = new blah();
}
The error message focuses on a[0] = new blah(); Telling me the 0 should be a ], the = is an illegal start of type, so on and so forth. The program compiles completely fine if it's within a method, like this:
class blah {
void a() {
blah a[] = new blah[7];
a[0] = new blah();
}
}
Or if I have public static void main (String[]args); But I'm trying to practice working outside of main.So does an array have to be within a method,
View Replies
View Related
Oct 20, 2014
I'm reading from one file and writing it into another.I then want to delete the first file and use the second (it will get the first files name) .My sample java file works completely fine but it doesn't work on my actual file -it looks like it populates my test file like it should but it does not delete the original. Occasionally DMap.xml won't delete either - I try and rename it and normally I get the error of 'the file is open in Java platform SE binary' so the files is open somewhere - but I thought I had closed it correctly so have I done that wrong? I have put the basic code :
public class DeleteEnvironmentsWindow extends JDialog {
String xmlFile = "c:DMap.xml";
String fromFile =xmlFile
public void removeEnvironment(){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Document doc = null;
[code]....
View Replies
View Related
May 3, 2015
Lets say in method 1 I want to print the numbers 1 - 10. Easy stuff. I then print that to the
PrintStream outFile = new PrintStream(new File ("output5_01")); I declared.
Now I'm in another methods, How do I print lets say 11- 20 without erasing the first methods printed results?
so my desired results would be
outFile
-------------------------------
(From 1st method) 1- 10
(From 2nd method) 11-20
As of now the second method always over writes the first. None of my books cover this and its very frustrating when the entire code is done but this won't let me write to the file
View Replies
View Related
Feb 14, 2015
I'm new to Java, and I just created this script:
public class HelloWorld {
public static void main(String[] args) {
String firstLine;
String startUp;
int hour, minute;
[code]...
Every time I try to run this is eclipse, I only get the first part, so it reads in the console: "Hello, world. The time is now 9:15. I'm tired."I want it to read: "Hello, world. The time is now 9:15. I'm tired. (new line) Today is Wednesday."
View Replies
View Related
Oct 20, 2014
know my code sample file of renaming a file and deleting an original file will work fine but when I use the code within my main java file - it appears to populate the file that I want populating fine but wont delete the origional. I have everything on my c drive so I can very easily keep track of what files I do and don't have.
Can using the same files in different methods cause problems? (Beginning Java forum at JavaRanch)
View Replies
View Related
Sep 24, 2014
I have a requirement , as part of that i have to get one UI value (JSP/JSF) , due to code avaialability the value i can't get in JSP directly for that i uses Javascript that value have to pass to JavaBean method which return some string. Codes have given below :
JavaBean method:
public void setExchangeCurrency(String exchangeCurrency) {
this.exchangeCurrency = exchangeCurrency;
}
/**
* @return the exchangeCurrency
*/
public String getExchangeCurrency(String cutryCode) {
ResultSet resultSet = null;
String sqlQuery = null;
[Code] ....
View Replies
View Related
Mar 1, 2015
I am trying to pass the values for UPPER_BOUND and LOWER_BOUND from the main method into the getValidNumber method. However, I'm not sure how to do this. The rest of the code is correct as far as I know, I just need to get the values for the bounds into the getValidNumber method. How would I do this? the notes in the main method explain what I need to do.
public static void main(String[] args) throws FileNotFoundException {
Scanner kb = new Scanner (System.in);
final double LOWER_BOUND = 0;
final double UPPER_BOUND = 100;
//Call the getValidNumber method, passing it the values LOWER_BOUND and UPPER_BOUND.
//Take the returned value and store it in a variable called num.
//Print out the value of num (here in the main method)
[Code] .....
View Replies
View Related
Mar 20, 2014
public class demo
{
Public class static void main(String[]args) {
//Creating a variable that will be a reference to the object
Peoples person_one;
[Code] ....
I have assembled this code below that has a void method which will creat a new object. Problem I encounter is that in
Create_object(person_one);
the person_one has an error saying not initialized. I'm jus trying to learn on my own ways here and practice so may know what's wrong with this? I know I can use a return object from methods but what about this approach?
View Replies
View Related
Jan 16, 2014
We know that all classes in Java extend the Object class. But methods in Object class are declared as public.I think if they were declared as protected, then also there wont have been any issue. So, what is the reason behind making them as public?
View Replies
View Related
Nov 18, 2014
The following code is supposed to generate random integers and sort array1 and array2 using two different sorting methods. array1 is to be sorted with a "selection sort" method and array2 is to be sorted with the built-in Arrays.sort() method. However, array1 is the one that has a problem. It does not appear to output any values at all for array size of 4000 or more, such as array1[10000]. The assignment is to generate random integers, sort and benchmark the speeds at which array1 and array2 can generate and sort ints at array1[1000] array2[1000] array1[10000] array2[10000] array1[100000] array2[100000]
/**
*
* The following is a sorting and benchmarking program to sort
* array1 and array2 with 1,000 , 10,000 and 100,000 array sizes.
* array1 uses selection sort from section 7.4 of the book
* and array2 uses the built in Arrays.sort() method.
[code]....
I cannot post the output because the amount of data seems to have crashed the two previous posts I made on this topic due to the size of the problem.
View Replies
View Related
Sep 13, 2014
Is it possible to have multiple methods in one class?And can I use the Scanner in methods other than the main method?
View Replies
View Related
Oct 17, 2014
I'm pretty new at java and I was wondering on how to get all my methods on one world frame?
The code is this:
public class TurtleTester
{
public static void main (String[] args)
{
World world= new World();
Turtle turtle= new Turtle(world);
turtle.drawRectangle(50,100);
turtle.drawHexagon(100);
[Code]...
When I run the main method, it would give me a bunch of world frames with one method in each one. I'm using BlueJ btw.
View Replies
View Related
Jan 4, 2015
I am receiving one error when using arrays and methods. It's at the bottom of the code and I have commented what the error is .
import java.io.*;
import javax.swing.*;
public class CEO_PROGRAM_PartB
{
public static void main (String[] args) throws IOException
[code]....
View Replies
View Related
Sep 24, 2014
The code is below. The program runs a series of calculations based on data input by the user. My problem is that for the most important thing I'm looking for, total kg CO2 emissions, I continually get an answer of 0.0. What I need is a sum of the individual total emissions as calculated in each method, i.e. the values which are printed with the following: System.out.println(trans); System.out.println(elec); and System.out.println(food);
The total should be something like 25040 or whatever, depending on the value of the inputs provided by the user, but I'm constantly getting a total of 0.0., which is obviously false. Could have something to do with the way I've initialized my variables, or something to do with the limitations of returning values from methods.
import java.util.Scanner;
public class CarbonCalc {
public static void main(String[] args) {
double trans = 0;
double elec = 0;
double food = 0;
giveIntro();
determineTransportationEmission(null);
[code]....
View Replies
View Related
Feb 5, 2015
So the first thing I would like to ask about is overriding inherited methods. I am asked to override methods in one of my assignments and I am not really sure how to go about doing it.
View Replies
View Related
Sep 14, 2014
I want to make a program where users are prompted to enter a username and a password and have these two values create a new instance of the Object User. But I'm not sure where to start.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
createUser();
[Code] ....
how to take username + password and put it into an object.
View Replies
View Related
Apr 30, 2014
I'm supposed to write a program that reads in 20 numbers stores them into a one dimensional array and then create a method that will calculate the average of the numbers in a separate method.
I've written a for loop in the main method that will take in the numbers but now I need to know how I can pass those values to a method that will calculate the average.
public class ConstructorHomework {
private static double average; //Declaring the Global Variable
//This Method Calculates The Average
public void avg(int x){
average = x/20;
[Code] .....
This is the main method an it contains the for loop that will take in 20 numbers
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int size = 20;
double[] array = new double[size];
for (int i = 0; i < array.length; i++){
array[i] = input.nextDouble();
}
//
Here I was trying to create and object and use it to pass value to the method
ConstructorHomework a = new ConstructorHomework();
a.avg(); //Problem is that I don't know what to put in the brackets
a.showNum();
}
}
View Replies
View Related
Sep 27, 2014
Write the methods with the following integers:
public static int reverse(int number)
//return true if a palindrome
public static Boolean isPalindrome(int number)
When I compile it says it cannot find symbol "remainder".
import java.util.*;
public class PalindromeInteger
{
public static void main(String[] args)
{
//Create a scanner
Scanner input = new Scanner(System.in);
[code]....
View Replies
View Related
Apr 14, 2014
I need to create a Dog class that represents a dog. Then, there are 2 different methods that in the end will each represent one dog each. Each printout will show the Name, Breed, Age, and Age in Human Years. Right now I'm having trouble trying to move methods in between each class. Here's the first class.
import java.util.Scanner;
public class Dog { //no main method
static Scanner input = new Scanner(System.in);
//writeObject method
public static void writeObject(String name, String breed, int age) {
System.out.println("Enter the dog's name, breed, and age: ");
[Code] ....
View Replies
View Related