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();
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) == '#'))) {
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.
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 );
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;
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.
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;
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.
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:
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?
// 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)
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())
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?
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
I am kind of new to programming in java and I am trying to run a first time setup type of thing for my program so I am saving the settings to a file. If the file doesn't exist I want it to run the first time setup but if the file does exist I want it to skip that process. My problem is that .exists() says reads true whether the file is there or is not there.
public static void main(String[] args)throws IOException{ new Window(); //Runs the Window() PrintWriter writer = new PrintWriter(path); boolean bool = new File("path.txt").exists(); System.out.println(bool); if(!new File("path.txt").exists()){
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?
public Unit(String code, String name) { enrolStudent(student); this.unitCode = code; this.unitName = name; } public void enrolStudent(Student newStudent){ students = new ArrayList<Student>(); newStudent = new Student(24662496, "Kingsley", " Iwunze"); students.add(newStudent); }
how can I call this enrolStudent() method on this Unit constructor in another class when I create a new Unit. all I need is to enroll students in units when units are created. below is my create unit method.
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?
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:
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");
I am currently working on a dice game. I have a private method called rollDice and it performs the action of rolling two dice. For my project, I need to create another method called playerRolls and I am supposed to invoke the rollDice method in the playerRolls method and perform actions based off of that. My question right now is how do I invoke a method into another method of the same class?
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);