Should I put my test method in a separate class? If so, how?
I am very new to Java. My assignment is to create my own method and then create a class to test it in. My question, do I create the method separate of the class, or within the class? If it is separate of the class, how do I get the class to access my method?
(Are they saved as two separate files?)
This is what I have so far, but I am getting an error that I have to initialize KILOWATT in class DWindmill. I thought I did already in the method??? Any suggestions?
//This is the method Windmill
import java.util.*;
import static java.lang.Math.*;
class DWindmill {
public static void Windmill(){
//create the method for the Windmill class
int miles = 50;
//int miles = 200;
//int miles = 250;
int KILOWATT = (miles / 50);}
static Scanner console = new Scanner(System.in);
{
System.out.println("Enter miles per hour:");
miles = console.nextInt();
Windmill();
System.out.println(+ KILOWATT + "kilowatts");
}
}
Asked by: Ryan274 | Posted: 23-01-2022
Answer 1
For a simple assignment such as this, you can probably create your method in the same class as your class. Create a class with a static main method, which will be your programs starting point, and then create your method which will be called.
Seems like you are quite new to programming I would take advantage of any tutorials that are offered in your program. They are usually taught by junior, senior, or grad level students, and are meant to give you a good introduction to the material, as well as give you time outside class to ask questions. Make sure you go to class, and try to read the textbook you were supposed to buy for the course. The information can often be found there.
Answered by: Edgar681 | Posted: 24-02-2022Answer 2
please go to class or read a textbook or something because your code illustrates a fundamental misunderstanding of what a class is, what a method is, and how to use braces for code blocks. Here is a corrected (but untested) version of your code -
class Windmill
{
public static void main(String args[])
{
Scanner console = new Scanner(System.in);
System.out.println("Enter miles per hour:");
int miles = console.nextInt();
int KILOWATT = (miles / 50);
System.out.println(KILOWATT + " kilowatts");
}
}
seriously, anything should be helpful at this point
Answered by: Dainton210 | Posted: 24-02-2022Answer 3
In Java, ALL methods exist within classes. So in order to create a class, you write something like:
public class MyClass {
public static void Hello() {
//This is your method!
}
public static void main (String[] args) {
Hello(); //This is how you call your method.
}
}
Answered by: David487 | Posted: 24-02-2022
Answer 4
OR you can create like follows
public class MyClass {
public int myMethod() {
,,,,,
}
}
public class myTest {
public void testMyMethod() {
MyClass testClass = new MyClass();
int output = testClass.myMethod();
.
.
}
}
In Java, all methods need to be inside a class. You can have a separate test class or test it in the same class.
Things can get more complicated if you use something like jUnit(www.junit.org) for unit testing your methods.
Answered by: Julian909 | Posted: 24-02-2022Similar questions
How to call a method from a separate class java
I'm trying to call a method from a separate class in java but it doesn't seem to work, or I'm doing something wrong. What I want to achieve is to call my Race method which is in the RacingEvent class to my main program (Check the comment in the main program).
Here is the class:
import java.util.Random;
public class RacingEvent {
private SimpleWindow w;
private Race...
Is there a way to call a method from a separate class in a Java GUI?
I am working on a project for a class. The project asked me to create 3 classes, one parent and two children, with 3 methods in each. It then asked me to create a GUI that would calculate the sales tax based on a few fields, including text and radio buttons. In my head, I would call my methods to run if a certain radio button is clicked, ie if the Hybrid button is clicked, it'll run the toString from the Hybrid class. Can ...
java - Should I inline long code in a loop, or move it in a separate method?
Assume I have a loop (any while or for) like this:
loop{
A long code.
}
From the point of time complexity, should I divide this code in parts, write a function outside the loop, and call that function repeatedly?
I read something about functions very long ago, that calling a function repeatedly takes more time or memory or like something, I don't remember it exactly. Can you al...
How to call a method from a separate class java
I'm trying to call a method from a separate class in java but it doesn't seem to work, or I'm doing something wrong. What I want to achieve is to call my Race method which is in the RacingEvent class to my main program (Check the comment in the main program).
Here is the class:
import java.util.Random;
public class RacingEvent {
private SimpleWindow w;
private Race...
java - How do you take user input in a separate method?
In my method "move" I am printing out instructions to the user. In the method "game" I need to take the user input and make the program do different things depending on the user input by using the method "usersMove". I originally had my user input scanned in the method "move" but that method has to stay as a void. Is there a way to take the user input in the "game" method so that I can apply its value to other methods?
java - Flags or a separate method for each flag possible value?
If you want to get at some point both negative or positive numbers from an array, which option would be 'better':
getAllNumbers(initialArray, boolean positive)
....
or
getAllPositiveNumbers(initialArray){
....
}
getAllNegativeNumbers(initialArray){
...
}
java - How can I make a method hide a window that's set up by a separate method
In this code, I am trying to get the window to hide away when I click the hide button. Clicking the button activates the makeHidden() method I have, but it's made on the same level as public static void main(String[] args), and that's where the window visibility is set. I can't seem to reference myWindow from the makeHidden method, and I don't know what put put before
java - Array in separate method
Closed. This question is not reproducible or was caused...
java - End the Game from a separate method
I have two different classes: TextBasedGame and IntroductionChoices.
IntroductionChoices makes all the decisions while TextBasedGame basically just prints the story (it's the one with the main method).
What I want to know is how to make the game END if the user makes a wrong choice.
In this case, if the user types in "Take down the group.", a message is displayed and the game ends. In my case, in the main method, i...
java - Close a Timer from a separate method
I have a Timer that works like this:
public void startTourButton(ActionEvent event) {
new Timer ().schedule(
new TimerTask () {
public void run(){
System.out.println("Going to do something here");
}
}, 0, 5000);
}
I am trying to cancel the Timerfrom a different event here:
...
Is there a way to call a method from a separate class in a Java GUI?
I am working on a project for a class. The project asked me to create 3 classes, one parent and two children, with 3 methods in each. It then asked me to create a GUI that would calculate the sales tax based on a few fields, including text and radio buttons. In my head, I would call my methods to run if a certain radio button is clicked, ie if the Hybrid button is clicked, it'll run the toString from the Hybrid class. Can ...
java - How to pass my ints from my Array as separate Ints in a Method?
I am currently working on a project where I created an Array with Ints to cover a playing field.
My problem is that I need to pass each value of the Array as a separate int to a method.
Example below:
int [] myArray = new int [9];
To the method:
public static void createFields(int f3, int f4, int f5, int f6, int f7, int f8, int f9, int f10, int f11)
I k...
java - Is there a way to separate long running (e.g. stress tests) out so they're not run by default in Maven 2?
We've had an ongoing need here that I can't figure out how to address using the stock Maven 2 tools and documentation.
Some of our developers have some very long running JUnit tests (usually stress tests) that under no circumstances should be run as a regular part of the build process / nightly build.
Of course we can use the surefire plugin's exclusion mechanism and just punt them from the build, but ideal...
java - In a class with two timers, do they execute in two separate threads?
In a java class I have two timers
TimerTask t1 = new TimerTask() {.. }
TimerTask t2 = new TimerTask() { ...}
Do t1 and t2 execute as two separate threads? How do you verify it?
How do I separate out query string params from POST data in a java servlet
When you get a doGet or doPost call in a servlet you can use getparameterxxx() to get either the query string or the post data in one easy place.
If the call was a GET, you get data from the url/query string.
If the call was a POST, you get the post data all parsed out for you.
Except as it turns out, if you don't put an 'action' attribute in your form call.
If you specify a fully qualified or ...
ms word - How do I use Apache POI to read a .DOC file in Java to separate images from text?
I need to read a Word .doc file from Java that has text and images. I need to recognize the images & text and separate them into 2 files.
I've recently heard about "Apache POI." How I can use Apache POI to read Word .doc files?
java - Separate DLL and native call in different plugin
I want to separate some DLLs from the associated native JNI classes.
Plugins:
In plugin A the dlls are placed and
loaded when the plugin is loaded.
In
plugin B (depend on A) the JNI
classes are placed which include the
native method calls for the DLLs in A.
At runtime i get a UnsatisfiedLinkError because the JNI class can't be found.
I try to update the classloader logic by up...
parsing - How can I separate tokens in Java when there are some null tokens
i have line in .csv file as
abc,bcc,
i have to separate it into three tokens as abc bcc and null
first i had try stringTokenizer but it will not return null token so
after that i try string.split(",") but it will not return the last null string
it will return string which has null in between but not at last
so please help me
thanks in advance.
Is "else if" one whole or two separate keywords in Java?
I happen to read the practicing material for SCJP certification, and I just tripped over a chapter of flow control et al, where they give the impression that "else if" is a keyword on its own. I have always thought that it was just a normal else, containing nothing but an if block, braces o...
java - Is it possible to separate totally the HTML/CSS layout from the GWT logic?
I'd like to allow our web developers to continue to work in pure HTML and to let developers to write GWT Java-only code to write the rest of the business logic.
Is it even possible?
Have anyone tried to work with the web developers in the GWT environment?
How do you incorporate the web developers into the GWT development process?
java - Eclipse open console apps in separate window
Is there a way to configure eclipse to open console apps in a new window rather than it's own console when you run/debug them?
I'm debugging a client/server application and I'd like to see the output of both apps at once and not have to switch between the tabs...
How can I execute a Windows batch file as a separate process from Java?
I'm writing a class in Java which will be calling a Windows batch file. When I run this class, the batch file is getting opened and getting closed. How can I make the batch file continue to run after the Java program terminates?
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)