How To Call A Class Within The Main
Apr 17, 2014
How do I call a class within the main. For example, this is what i have in my code right now. I am trying to call the class BlackJackGame.
package blackjack;
public class BlackJack {
public BlackJack() {
BlackJackGame();
}
}
View Replies
ADVERTISEMENT
Dec 9, 2014
The following code is located in my main class called InjectionFix. I have another class where I need to call or initiate the string in the code below. How can I achieve this. I tried to create an Object of the class but I cant do this because my other class doesnt contain a main method.How can I get the other class to initiate the code below which is loacted in my main class.
public static String escapeDN(String name) {
StringBuilder sb = new StringBuilder();
// space or # character at the beginning of a string
if ((name.length() > 0) &&
((name.charAt(0) == ' ') ||
(name.charAt(0) == '#'))) {
[Code] .....
View Replies
View Related
Dec 19, 2014
For reference I am programming Java in BlueJ. I am fairly new to the language and I am having trouble with sorting.
I am trying to call / test all of the 5 sorting methods (at the same time) in the main class. To be specific, the sorted list has to technically outputted 5 times.
I figured out how to call / test Quicksort:
Sorting.quickSort(friends, 0, friends.length-1);
But the others are not working correctly. Specifically these:
Sorting.mergeSort(friends, 0, friends.length-1);
Sorting.PbubbleSort(friends, 0, friends.length-1);
Sorting.PinsertionSort(friends, 0, friends.length-1);
Sorting.selectionSort(friends, 0, friends.length-1);
For reference, this is the output when it is not sorted:
Smith, John 610-555-7384
Barnes, Sarah215-555-3827
Riley, Mark 733-555-2969
Getz, Laura 663-555-3984
Smith, Larry464-555-3489
Phelps, Frank322-555-2284
Grant, Marsha243-555-2837
This is the output when it is sorted:
Barnes, Sarah215-555-3827
Getz, Laura 663-555-3984
Grant, Marsha243-555-2837
Phelps, Frank322-555-2284
Riley, Mark 733-555-2969
Smith, John 610-555-7384
Smith, Larry464-555-3489
This is the class Sorting, which I should note is all correct:
public class Sorting{
/**
* Swaps to elements in an array. Used by various sorting algorithms.
*
* @param data the array in which the elements are swapped
* @param index1 the index of the first element to be swapped
* @param index2 the index of the second element to be swapped
*/
private static <T extends Comparable<? super T>> void swap(T[] data,
int index1, int index2){
T temp = data[index1];
data[index1] = data[index2];
[Code]...
This is the Main class in which I am supposed to call the sorting methods, SortPhoneList:
public class SortPhoneList{
/**
* Creates an array of Contact objects, sorts them, then prints
* them.
*/
public static void main (String[] args){
Contact[] friends = new Contact[7];
friends[0] = new Contact ("John", "Smith", "610-555-7384");
friends[1] = new Contact ("Sarah", "Barnes", "215-555-3827");
[Code]...
View Replies
View Related
Feb 13, 2014
I am just trying to test this array, for a locker combination program that involves classes...but the array is printing out the whacky numbers for the location. When I try to call the method in the main, it does not work. How do I call a method that exist within a class into the main method?
public class locker {
public static void main(String[] args) {
CombinationLock();
[code]....
View Replies
View Related
Jul 3, 2014
I am trying to call a constructor from PrepaidCard class in my main method, but I am not sure how to proceed.
As seen below, both the PrepaidCard constructor and the setCardID method have the ability to set the card ID.
public class PrepaidCard
{
public PrepaidCard(String id, int token) {
cardID = id;
tokenBalance = token;
}
public void setCardID(String id, int token) {
cardID = id;
tokenBalance = token;
}
}
Now in this block of code, I can successfully pass the id and token value by calling the setCardID method from the PrepaidCard class. Now I would like to call the PrepaidCard constructor from the PrepaidCard class to pass the id and token value, instead of using the setCardID method.
public class PrepaidCardTest
{
public static void main(String[] args) {
PrepaidCard card2 = new PrepaidCard(id, token);
System.out.print("Enter Card2 cardID: ");
id = input.nextLine();
card2.setCardID(id, token);
}
}
How to call the PrepaidCard constructor from the PrepaidCard class, to successfully pass the id and token value, in my main method?Specifically how to modify or replace this line of code so that it can correctly call the PrepaidCard constructor?
card2.setCardID(id, token);
View Replies
View Related
May 7, 2015
I can call a child method from a main method. If I have a parent called "Assessment", a child called "Quiz", and another child called "test". Can I instinate an object like Assessment a = new test(); and call a method in test.I know the method can be called if I instinate the test t = new test() but that defeats the purpose of inheritance which I'm learning in class.
View Replies
View Related
Jul 3, 2014
I am trying to call a constructor from PrepaidCard class in my main method, but I am not sure how to proceed.
As seen below, both the PrepaidCard constructor and the setCardID method have the ability to set the card ID.
Java Code:
public class PrepaidCard {
public PrepaidCard(String id, int token) {
cardID = id;
tokenBalance = token;
} public void setCardID(String id, int token) {
cardID = id;
tokenBalance = token;
}
} mh_sh_highlight_all('java');
Now in this block of code, I can successfully pass the id and token value by calling the setCardID method from the PrepaidCard class.
Now I would like to call the PrepaidCard constructor from the PrepaidCard class to pass the id and token value, instead of using the setCardID method.
Java Code:
public class PrepaidCardTest {
public static void main(String[] args) {
PrepaidCard card2 = new PrepaidCard(id, token);
System.out.print("Enter Card2 cardID: ");
id = input.nextLine();
card2.setCardID(id, token);
}
} mh_sh_highlight_all('java');
How to call the PrepaidCard constructor from the PrepaidCard class, to successfully pass the id and token value, in my main method?
Specifically how to modify or replace this line of code so that it can correctly call the PrepaidCard constructor?
Java Code: card2.setCardID(id, token); mh_sh_highlight_all('java');
View Replies
View Related
Nov 27, 2014
So I have been given this code, below:
public class Person{
/*Complete*/
public String format(){
return String.format( "Name: %s", name );
[Code] ....
And I have been asked to add a new field called name, and call the display method in the main. I need to use an appropriate type and privacy for the name field, and once the code is finished, I should see 'Harry Potter' appear on the command line. However I don't know what it means by field. I've tried googling it and one search result said it is a field parameter, and another search result said it was something completely different, and I don't know which one it is. I've created a parameter 'Private String name;' but it only prints out 'name: null'
This is the code I wrote but it obviously doesn't work?
package Practical8;
public class Person{
Private String name;
public String format(){
return String.format( "Name: %s", name );
[Code] ....
View Replies
View Related
Apr 27, 2015
I have this program, I am wondering if it is possible to call files from the main method and sort them into my saveOneRocord method? If so, how would that look?
my orderProcess Class
package stu.paston.finalprogram;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileWriter;
[Code] .....
View Replies
View Related
Mar 25, 2015
I am trying to make a 2d graphical animation using the java swing classes in order to make a frame. I had a basic version working when all of the code was under the main method, but when I moved some into another method it broke it. With y understanding of java my code should work as I create a variable of the method containing the code and then assign the size and exit button. However this causes many problems such as my BeaconFrame method informing me that it is unused when I have used it. Here is my code:
import javax.swing.*;
import java.awt.*;
public class BelishaBeacon {
public void BeaconFrame() {
JFrame frame = new JFrame();
JPanel panel1 = new JPanel();
[code]....
View Replies
View Related
Aug 27, 2014
For the below program what are the default values passed by the JVM in order to call main() method
class program
{
public static void main(String[] args)
{
System.out.println(args[0]);
System.out.println(args[1]);
}
}
View Replies
View Related
Jan 8, 2014
I've 3 classes.
1. Circle
2. GetInputFromUser
3. testCircle
package ABC;
public class Circle {
private double radius;
public double getRadius() {
return radius;
[Code] .....
In the testCircle class, in the line: getRadius = ui1.GetInput();
It's showing the error: The method GetInput(float) in the type GetInputFromUser is not applicable for the arguments ()
And when I do: getRadius = ui1.GetInput(rad);
It's showing the error: rad cannot be resolved
View Replies
View Related
Aug 1, 2014
So i declared a class in main class but it seems there's error when i compile:
constructor xx in class xx cannot applied to given types
This is my java class:
public class trainer extends person{
String classType;
public trainer(String name, String gender, String address, int id, String classType) {
super(name,gender,address,id);
this.classType=classType;
[Code] ....
And this is the way i declared in main class:
trainer tr = new trainer();
And what i want to do is:
tr.toString();
View Replies
View Related
Apr 1, 2015
public class Sphere {
public double diameter;
public double volume;
public double area;
public double fourThirds = 4/3;
public Sphere(double someDiameter){
someDiameter = diameter;
[Code] ....
I am trying to get this code so that I only enter the diameter once in the sphere object1 = new Sphere(4); but I can't get it to work right. I can get the diameter to work with the calculate volume and area methods but that's it.
View Replies
View Related
Mar 3, 2014
Im writing a simple program to understand classes and objects. Basically what I have is a file called Program.java where I have my main method.I have another file called Person.java which I want to use to create Person objects. That person can have a name, email adress, phone number, etc.I put both these files in the same folder.in Program.java my first statement is:
Java Code: import Person.java mh_sh_highlight_all('java');
My problem is that when I compile Program.java i get an error message saying that the package Person.java does not exist.So my question is, when you create a class that you want to use for objects, how do you import that class into your class with the main method so that you can use instances of your other class?
View Replies
View Related
Aug 31, 2014
// Add range to Vehicle.
class Vehicle {
int passengers; // number of passengers
int fuelcap; // fuel capacity in gallons
int mpg; // fuel consumption in miles per gallon
// Display the range.
void range() {
System.out.println("Range is " + fuelcap * mpg);
[Code] ....
I'm compiling it in Eclipse and this continues to show in the console display
Minivan can carry 7. Exception in thread "main" java.lang.NoSuchMethodError: Vehicle.range()V
at AddMeth.main(AddMeth.java:34)
View Replies
View Related
Oct 30, 2014
Need to use a ComboBox from another class (Beginning Java forum at JavaRanch)
When my file is saved it has the values off the main gui's comboBox in variable 'env' which I then write to file using. I just need to access the comboBox in my second class so I can use it in main as I'm using the wrong one atm- if that makes sense?
View Replies
View Related
May 31, 2014
I have a program with 4 classes, all of them in the same package, one of them is the Main class, and in that class I declared a variable named "port" of type int. One of the 3 another ones is the class Connection class, which it requires the port variable. I want to use this variable in the Connection class. How can I do it?Both classes are shown below:
Main.java
package server;
/* Imports */
/* Another variables */
int port; /* <-- IS THIS ONE */
[code]....
View Replies
View Related
Feb 5, 2015
THIS IS MY MAIN CLASS:
Java Code:
import java.io.FileNotFoundException;
public class PrimaClasse {
public static void main(String[] args) throws FileNotFoundException {
SecondaClasse oggettoSeconda = new SecondaClasse();
oggettoSeconda.controlloNomi();
[code]....
Now it's working and from the main class i can controll the second class BUT. i want that is the main class that ask the user name, and i want pass that value to the second class. what code i must change?why eclipse wants me insert this import java.io.FileNotFoundException; and this throws FileNotFoundException for not give me an error?
View Replies
View Related
Mar 5, 2014
I am getting the problem as If call the action class through the ajax, my property values not setting to the formbean class, How can I use the formbean to set the all my property values and getting in action class.
View Replies
View Related
Sep 28, 2014
I am trying to make a JTree. I have used this guide :[URL]
Now when I call my Class with
TreeMainMenu tree = new TreeMainMenu();
JScrollPane MainMenu = new JScrollPane(tree);
I get only the default JTree..
I need to understand how I should build my code to call the class as JTree.
This is my code:
public class TreeMainMenu extends JTree {
private DefaultMutableTreeNode top = new DefaultMutableTreeNode("TOP");
JTree tree;
public JTree TreeMainMenu() {
APNode();
tree = new JTree(top);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
return tree;
[code]....
View Replies
View Related
Mar 17, 2015
I have wrote the necessary program for the class which was : Modify the customer class to include changeStreet(), changeState(), and changeZip methods. Modify the account class to include a changeAddress() method that has street city and zip parameters. Modify the Bank application to test the changeAddress method.
The problem arose when I went to test it. For some reason when it asks "Would you like to modify your account information? (y/n)" it will not allow the user to input anything and thus test the class. Here is my code
Class Customer
import java.util.*;
import java.io.*;
public class Customer {
private String firstName, lastName, street, city,state, zip;
[Code] ....
View Replies
View Related
Dec 23, 2013
I am newbie in java and little bit known to apex. I write an java code and compile it to class file. Now i want to call that class file from an push of button on apex. When button is pushed i need some arguments to be password to java class files . For arguments i need to take the item value from the apex page.
But i stuck on how to call that java class file from apex. On command prompt when i ran java class file, its working.
View Replies
View Related
Aug 7, 2014
I have 3 source file below
//Hello.java
public interface Hello {
public void sayHello();
}
//Espanol.java
public class Espanol implements Hello {
[Code] ....
View Replies
View Related
Mar 27, 2014
If I declare and ArrayList as follows
"public static ArrayList<Media> mediaList = new ArrayList<Media>();"
How can i access it in another class (main class)?
package Projekt;
import Projekt.Media.rating;
public class MainTest {
static MediaHandler myreg ;
public static void main(String[] args) {
myreg.addMovie(title, playTime, year, seen, directory, path, rating.four, quality, subtitles, language, writer);
}
}
I also have the variables declared but didn't include them for readability.
View Replies
View Related
May 21, 2014
I had the question of what this was. [URL] ....
In summary, the Class XCopy's Main method creates an instance of the XCopy Class. So, now knowing that this can and does occur my next questions to myself were:
When would I do this?
How can this best be used?
Is this just another option available to a Java developer that has no other special significance?So far have no answers for myself.
View Replies
View Related