Access A Method Of Class From Main

Feb 9, 2014

This time I have to make a Black Jack game ( I guess this is a classic) I have created Three classes for this BlackJack( Main), Card, and Player.

What I am trying to do is put the Give one card to the player and remove it from the deck into a separate procedure because I will be doing this several times during the game.

This is the code I have so far Under the class BlackJack.

Java Code:

package black.jack;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

[Code] .....

View Replies


ADVERTISEMENT

Error Passing Value Of A Variable From One Class To Main Method Of Another Class

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

How To Call A Method That Exist Within A Class Into Main Method

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

How To Import Class Into Class With Main Method

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

Calling Method Of A Class From Main Class?

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

How To Access Getter Method From Different Class

Sep 16, 2014

I have 2 classes. TestClassA has 1 getter and 1 setter. From my second class, TestClassB, I want to access the getter method of TestClassA. If I try to access it, I get null. How do I do it?I do not want the methods to be declared as static. How can the getter method value be printed in TestClassB (without marking anything as static)?

public class TestA {
private String name;
public String getName() {
return name;

[code]....

View Replies View Related

Why Cannot Access A Public Method From Another Class

Feb 12, 2015

Why can't I access a method from another class? For example, I want to get the value of get method from another class. It's giving me an error on if(getExamType() == 'M') That's what I've done, for example:

Java Code:

public static Exam[] collateExams(Exam[] exams){
Exam [] r = new Exam[50];
r = exams;
Exam [] finalExam = new Exam[50];
for(int i = 0; i < r.length; i++) {
if(getExamType() == 'M') {
}
}
return r; mh_sh_highlight_all('java');

View Replies View Related

Splitting Up Main Method / Class?

Jan 29, 2014

I've written a program just for the sake of it (to learn) and it seem's like theres quite a lot in the main method that perhaps could be split up into seperate classes. I'm not too sure where should start with this though, or what my thought process should have been as I was writing the program.

import java.util.Scanner;
 public class Loops {
 public static void main(String[] args) { 
int answer = 16; 
Scanner scan = new Scanner(System.in); 
// Question
 System.out.println("What is 4 x 4 ?"); 

[code]...

--- Update ---

here's a version without code comments as they might make it harder to read here -

import java.util.Scanner;
 public class Loops {
 public static void main(String[] args) {
 int answer = 16;
 Scanner scan = new Scanner(System.in);
 System.out.println("What is 4 x 4 ?");
 int userAnswerInt = 0;

[code]...

View Replies View Related

How To Get Access To The Method With A Parameter Of Class Type Variable

Feb 19, 2014

how to get an access to the method with a parameter of class type variable, lets say: public void insert(Record newRecord, int pos)?

View Replies View Related

Create Method In Main Class And Use It On Object

Feb 26, 2015

I've been writing classes over and over for school. So I create a class outside of my main class. I create a new constructor and then create objects from my main class. I hope that makes sense. So i use methods in that class to work with the object. So I have an object name I've created <dot> method name. So I can create objects and then use methods from the class, but I'm wondering can I create a method in my main class and use it on that object? I don't understand how to do that.

View Replies View Related

How To Call String From Main Method In Different Class

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

Is It Correct To Declare A Class Inside Main Method

Jan 21, 2014

I saw an example where an (inner)class is declared inside the main method, this is correct or not and why/when it's reasonable to use?so smth like this

public class myClass()
{
public static void myMethod(myInnerClass obj)
{
if (obj.method())

[code]....

View Replies View Related

Binary Search Tree - Calling FindSmallest Method In Main Class

Nov 3, 2014

How should I call my findSmallest method in the main class.. Here is the code:

public class Tester {
public static void main(String[] args){
try {
BinaryTree<Integer> myTree2 = new BinaryTree<Integer>();
 
myTree2.insert(5);
myTree2.insert(2);
myTree2.insert(7);

[Code] ....

So the question is what kind of parameter I should pass in myTree2.findSmallest()??? Here is my findSmallest method..

public E findSmallest(Node<E> parent){
if(parent.left.left == null){
E returnValue = parent.left.data;
parent.left = parent.left.right;
return returnValue;
} else {
return findSmallest(parent.left);
}
}

View Replies View Related

How To Return Array From A Method / Back Into Main Method That Prints Out Stuff

May 27, 2014

I'd like to know how to return a new array, I wrote in a method below the main method. I want to print the array but system.out.print doesn't work for arrays apparently. What structure i should use?

View Replies View Related

Writing A Method That Returns Words Without Four Letters Back To Main Method

May 27, 2014

I have to write a method called censor that gets an array of strings from the user as an argument and then returns an array of strings that contains all of the original strings in the same order except those with length of 4. For example if cat, dog, moose, lazy, with was entered, then it would return the exact same thing except for the words with and lazy.

Currently, my code so far just prints [Ljava.lang.String;@38cfdf and im stuck.

import java.util.Scanner;
 public class censorProgram {
public static void main (String args[]){
Scanner input = new Scanner (System.in);
System.out.println ("How many words would you like to enter?");
int lineOfText = input.nextInt();
 
[Code] ....

I have to make new string array in the method and return words without four letters in the main method

View Replies View Related

Access Getter / Setter Of Bean Class Where It Defined As Member In Another Class?

Feb 18, 2014

Class UserAssessBean{
private String username;
private int userid;
private ArrayList<ModuleBean> module;
--{get/set}--

[Code] ....

How can i access the getters/setters of module bean, when it was returned as array list in UserAssessBean?

View Replies View Related

Declaring Class In Main Class - Constructor Cannot Applied To Given Types

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

How To Enter In Diameter In Main Class For Sphere Class

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

Main Method Separated From Other Method

Jan 13, 2014

Here's a class called Fibonacci

Java Code:

public class Fibonacci {
public static int fib(int n) {
if (n < 2) {return n;}
else {return fib(n-1)+fib(n-2);}

[code]....

it contains one method called fib() and one main method.If I would want to have the main method in another class than fib(), how would I write the two classes? Only cutting the main method from this class to another one doesn't work.My question is, is the reason it doesn't work because I then would have to have a constructor in the Fibonacci class, and create a Fibonacci object first which I then use the method on?

View Replies View Related

How To Access Keylistener If It Is Within A Method

Feb 1, 2014

I am trying to make a program where a ball moves up continuously when you press space, then begins to move down when you reach a certain point. My method for this is to have a timer, and have a method that does this: When you press space, add 10 y coords every second (using a timer), and if you reach 470 y, then begin to drop 10 y coords. I made a method to hold the keylistener, and am running that method inside the actionPerformed method which is within another class. However, since it is a method, I cannot add my keylistener to my frame in the main method.

main
error line 9
Java Code: import javax.swing.*;
public class Main {

[code]...

View Replies View Related

Using ComboBox In Main Class From Another Class

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

Using A Variable From Main Class In Another Class

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

Pass A Value From Main Class To Another Class

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

Polymorphically Access A Protected Method

Feb 1, 2015

Why is it that a protected method cannot be accessed polymorphically outside its package. I suspect the reason is that some compile-time checks have to be performed. Is this correct?

View Replies View Related

No Main Method?

Jul 23, 2014

I'm trying to make a TBG (Text Based Game) in Java for Android. Now, when I try to run it, it says No Main Method to run this program. The compiler also checks the program and tell me there are no errors.

View Replies View Related

Access To Variables In Another Class?

Oct 4, 2014

I will like to add to the questions about constructors and its this. I have a class A with a constructor, i have a class B which initialize the constructor. also i have a class C which needs a variable in class A but doesn't need to initialize the constructor of A. the question how do i access the variable of class A without initializing the constructor.

View Replies View Related







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