-1

I know this is a repeated question but I don't really understand how to call upon the functions or the utility of all the privates etc... So what I'm trying to do is "build a bear" where u pick the size and colour in a different class while the main class calls it, like this:

Size

import java.util.Scanner;

public class BABSize 

{

    public static String size()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Would you like a small, medium or big bear?");
        String size = input.nextLine();
        input.close();
        return size;
    }
}

Colour

import java.util.Scanner;

public class BABColour 
{

    public static String Colour()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("What colour would u like your bear?");
        String colour = input.nextLine();
        input.close();
        return colour;
    }
}

Main

public class MainFunction 
{
    
    public static void main(String[] args) 
    {
        BABColour c = new Colourr();
        BABSize g = new Size();
        System.out.println("Your " + g + "," + c + " bear will be ready in a moment:)");
    }

}
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Dxher
  • 1
  • Do you understand how [static methods](https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) work in java? – Abra Sep 25 '20 at 19:08

2 Answers2

0

Since you are making static functions, you dont need to create instance of those classes and directly call the static methods. Not suggested but you can still have a call after creating instance of it and using that reference to call the method.

Try to observe what you are returning fron that function and have that reference in calling method. For example: For asking which beer, you are returning String. So, in main method have that reference of String. String beer = BABSize.size(); Similarly for colour you are retuning String so have that in main method.

p285
  • 1
  • 1
  • 3
0

change it like this

public static void main(String[] args) 
{
    BABColour c = new BABColour();//here you create a variable of type Colour
    BABSize g = new BABSize();
    String color = c.Colour();//Here you call the method color
    String size = g.Size();//Here you call the method to get the size
    System.out.println("Your " + color + "," + size + " bear will be ready in a moment:)");
}

(Im open to any question you have obout this)

Holis
  • 175
  • 1
  • 10