0

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; 
4
  • Define "call the classes". You are having trouble running the program after compiling it? Commented Nov 1, 2013 at 16:09
  • There is a problem with asking a question when you don't know the terminology yet: you can't "call classes", you can only call methods, which are defined in classes, and are associated with objects (unless they're static, then they're associated with single classes). Can you rephrase your question to use the proper java terminology? Commented Nov 1, 2013 at 16:09
  • You do not call classes. Create main() - you can do it in the same class. Then create an object of the class. Then call methods using obj.method(). Commented Nov 1, 2013 at 16:10
  • Sorry, meant to say methods. Commented Nov 1, 2013 at 16:13

5 Answers 5

2

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(); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help! Can you check out the new error I'm getting?
1

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.

2 Comments

Thanks for the help! Can you check out the new error I'm getting?
@user2943817: Sure, can you post the whole class you are trying to run?
1

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

For example:

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

Comments

0

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; 

1 Comment

Thanks for the help! Can you check out the new error I'm getting?
0

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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.