user2943817
user2943817

Reputation: 55

Basic Java: How to call a method?

I'm trying to create a program that includes a menu and will execute whatever choice the user chooses. I completed the methods and got them to compile, but am lost on how to call the classes. Here is the code in screenshots so they're easier to read:

Geek Class: (Contains all the methods)

    public class Geek{
    private String name;
    private int numberofQuestions=0;

    public Geek (String name){
        this.name = name;
        numberofQuestions = 0;

    }
    public String getName(){
        return name;

    }
    public int getnumberofQuestions(){
        return numberofQuestions;
    }
    public boolean allTheSame(int num1, int num2, int num3){

        numberofQuestions++;
        if(num1 == num2 && num2 == num3 && num1 == num3){
            return true;}
            else return false;
        }
    public int sum (int num1, int num2){
        numberofQuestions++;
        int largest = Math.max(num1, num2);
        int smallest = Math.min(num1, num2);
        int result =0;
        for (int i=smallest; i <= largest;i++){
        result = result + i;}
        return result;

    }
    public String repeat(String str, int n){
        numberofQuestions++;
        String repetition = "";
        for (int j=0; j < n; j++){
        repetition = repetition + str;}
        return repetition;

        }


    public boolean isPalindrome(String str){
        numberofQuestions++;
        int n = str.length();
        for( int i = 0; i < n/2; i++ )
        if (str.charAt(i) != str.charAt(n-i-1)) return false;
    return true;
    }

}

Main:

https://i.sstatic.net/E2z3a.png

EDIT: Im getting a cannot find symbol error in this section of code:

case "d":
        myGeek.sum(num1, num2, num3);
        System.out.println("Enter the first number");
        int num1 = scan.nextInt();
        System.out.println("Enter the second number");
        int num2 = scan.nextInt();
        System.out.println("Enter the third number");
        int num3 = scan.nextInt();
        break;

Upvotes: 0

Views: 686

Answers (5)

r0t0xd
r0t0xd

Reputation: 206

You need to instantiate an instance of the class in order to use it.

For example, in your main method you may want to define an instance of the object like so:

Geek myGeek = new Geek("user2943817");

You can than access all instance methods on the object using the variable name like so:

myGeek.getName();

I hope this helps!

EDIT :

As for your new issue, simply call the method after you obtain the values from the user. Like this:

case "d":
    System.out.println("Enter the first number");
    int num1 = scan.nextInt();
    System.out.println("Enter the second number");
    int num2 = scan.nextInt();
    System.out.println("Enter the third number");
    int num3 = scan.nextInt();
    myGeek.sum(num1, num2, num3);
    break;

Upvotes: 0

alex2410
alex2410

Reputation: 10994

You need to create instance of object and then you can use it

For example:

Geek g = new Geek("Geek");
g.getName();

Upvotes: 1

libik
libik

Reputation: 23029

You cant use non-static class like that.

Imagine that Geek is definition of Geek. You want to create several geeks, each one is standalone instance.

Geek g1 = new Geek("Andrew");
Geek g2 = new Geek("John");

These lines create two instances g1 and g2 of type Geek with name Andrew and John

Then you access them like this :

g1.repeat("myString", 10)

Upvotes: 0

Ross Drew
Ross Drew

Reputation: 8246

Classes aren't called, they are blueprints for creating objects.

You need a program entry point, in this case inside your class like so

public static void main(String[] args)
{
  Geek myGeekObject = new Geek("Your name");
}

you can then call the methods on your created object

public static void main(String[] args)
{
  Geek myGeekObject = new Geek("Your name");
  String geekName = myGeekObject.getName();
}

Upvotes: 2

tilpner
tilpner

Reputation: 4328

In Java (and any other languages I know), you can only call methods/functions, but not classes (except you count <clinit>). So you could write:

Geek geek = new Geek("Me");
int i = geek.sum(1, 2);
System.out.println(String.valueOf(i));

Assuming the file Geek.java, you can then call/run the class using:

javac Geek.java
java Geek

from the commandline.

Note that this will only succeed if Geek.java contains a main method. Read about them here.

Upvotes: 1

Related Questions