Cannot Call Child Method From Main

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


ADVERTISEMENT

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

Add New Field And Call The Display Method In Main

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

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

Default Values Passed By JVM In Order To Call Main Method

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

Calling Child Methods From Main

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 instantiate an object like Assessment a = new test(); and call a method in test.

I know the method can be called if I instantiate the test t = new test() but that defeats the purpose of inheritance...

View Replies View Related

Can't Navigate From Child To Main Panel

Apr 4, 2014

i have created two(displaypanel & buttonpanel) main panels in a JFrame and many child panels,one of the panel is for holding buttons and displaypanel mainly swap child panel as directed from buttonpanel, but the problem arises i cannot navigate from child panel to another child panel,

as i have made a button on one of a child panel and from button panel i add the childpanel to displaypanel.it is working but when i tried to navigate from the button which is on child panel nothing happened. "i have made a function in main form which swap the content of mainpanel (displaypanel) and in childpanel i have acces the function through object of mainform"

View Replies View Related

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 View Related

How To Call A Constructor In Main

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

Prepaid Card - How To Call A Constructor In Main

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

Call Variables From Main To Write To External File

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

Java Swing Frame Can't Call On Other Methods From Main

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

Parent / Child Classes - Set And Get Method?

Jan 25, 2014

I have been working on a simple problem, but I am stuck. I am trying to learn parent and child classes and how they work. The program in broken into three classes; the DemoBook class that runs the various methods, the Book class that gathers information and displays it, and finally a child class of Book (called TextBook) that just gets one piece of data and then is suppossed to return that data back to Book. However, this is not working and I know I am missing something; I believe it has to do with Set and Get methods, but I am confused with how these work.

Java Code:

public class DemoBook
{
public static void main (String[] args)
{
Book aBook = new Book();
Textbook aText = new Textbook();

[Code] .....

View Replies View Related

Unable To Call / Test All Of The 5 Sorting Methods At Same Time In Main Class

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

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

Method Call For A Void Method

Sep 29, 2014

I've never had to do a void method call. I have my void method in one class and my main (where I want to do the call) in another. How do you actually call a void method?

View Replies View Related

How To Call Self Method

May 18, 2014

How to call/define/describe self method.. below is my code :

Java Code:

public static void main(String[] args) {
banana1();
banana2();
banana3();
banana4();
banana5();
banana6();

[code]....

how exactly to call a self method ?

View Replies View Related

Accessing Parent Class Method Using Child Class Object?

Feb 4, 2015

I want to know is there any way we can call parent class method using child class object without using super keyword in class B in the following program like we can do in c++ by using scoop resolution operator

class A{
public void hello(){
System.out.println("hello");
}
}
class B extends A{
public void hello(){
//super.hello();
System.out.println("hello1");

[code]....

View Replies View Related

How To Call Variable From Another Method

Oct 3, 2014

My code has a method where the users input a bunch of variables and then those variables get added together in a new variable. What I want to know is how do I call the variable that is in the other method to another method?

import java.util.*;
public class Calc{
public static void main (String [] args){
determinevalue1();
determinevalue2();
determineTotalvalue(double value1, double value2);

[Code] ....

View Replies View Related

Can Call A Method In If Else Statement?

Dec 6, 2014

I am trying to get the program to ask the user to enter the project grade and if it is less than 65, then I want the program to display "fail" or if it is greater than 64 than I want it to display "passed". In this project I have to include a method and I am trying to call it in my if-else statement. I keep getting an error message saying "Project.java:143: error: incompatible types: void cannot be converted to int". I posted my code below. It's a long program, but this part is toward the bottom.
 
import java.util.Scanner;
public class Project {
public static void main(String[] args) {
  //call method
welcomeMessage();
 
[Code] ....

View Replies View Related

Method Call Not Working?

Oct 30, 2014

I have two comboBoxes - one in main and another in my 'windows' class. The code below is in main and references the comboBox in main but I need to use the comboBoxEnv out of my 'windows' class. How can I do that so it says ComboBoxEnv rather than comboBox?

JMenuItem menuExport = new JMenuItem("Export");
menuExport.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(comboBox.getSelectedItem() == null){

[Code] ....

I've gotten access to it by changing my code and making a method of it but I'm not sure of what I do now

exportImport.comboBoxEnv();

View Replies View Related

How To Call Method Without Using Inner Classes

Jun 9, 2014

How do i call the method without using inner classes in this example:

jt = new JTable (model) {
public boolean isCellEditable (int row, int col) {
if (col == 5) {
return false;

[Code] ....

View Replies View Related

To Call Java Method In JSP

Apr 28, 2014

I am trying to call a java method in jsp. The main idea is to hide menu based on the user who logs in. The java class flows like this :

public class UserVerification {
public static void main(String[] args) {
UserVerification obj = new UserVerification();
System.out.print(obj.GetUserVerification("abc"));

[Code] ......

View Replies View Related

How To Call Increment Method

May 26, 2015

I have the following:

public class HistoricalMoment{

private String eventName;
private ClockDisplay timeOfEvent;
public static final int MIDNIGHT_HOUR = 00;
public static final int MINUTE_ZERO = 00;
}

How can I create a method called public void addMinuteToTimeOfEvent() which calls the timeOfEvent's increment() method to add one minute to the timeOfEvent?

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







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