3

I am working on using separate classes in a Object-Oriented Programming program.

  1. Would you create the separate class and the program itself in separate .java files or the same?

As in:

public class filename {
    // ... content
}

class name {

}
  1. Can you use the Scanner within the separate class, or should you use it in the main() method?
    If so, how?

  2. Suppose you have to use the Scanner in the main() method, and you declared a new variable input and initialized it as the Scanner input.
    How would you set that as one of the variables in the separate class? Would you make a new object and do.

Example:

public class TestSimpleCircle {
    public static void main(String[] args {
    SimpleCircle circle1 = new SimpleCircle();
    System.out.println("The area of the circle of radius " + circle1.radius + " is "  + circle1.getArea());

    // Create a circle with radius 25
    SimpleCircle circle2 = new SimpleCircle(25);        System.out.println("The area of the circle of radius " + circle2.radius + " is " + circle2.getArea());

    // Create a circle with radius 125
    SimpleCircle circle3 = new SimpleCircle(125);       System.out.println("The area of the circle of radius " + circle3.radius + " is " + circle3.getArea());

    // Modify circle radius
    circle2.radius = 100; // or circle2.setRadius(100)  System.out.println("The area of the circle of radius " + circle2.radius + " is " + circle2.getArea());
    }
}

class SimpleCircle {
    double radius;

    SimpleCircle() {
        radius = 1;
    }

    SimpleCircle(double newRadius) {
        radius = newRadius;
    }

    double getArea() {
        return radius * radius * Math.PI;
    }

    double getPerimeter() {
        return 2 * radius * Math.PI;
    }

    void setRadius(double newRadius) {
        radius = newRadius;
    }
}

...that (e.g. circle1.getArea())?

  1. When you create variables in a separate class, are they in any way connected to variables with the same name in the main() method or a different class?
Ola Ström
  • 4,136
  • 5
  • 22
  • 41
matematika
  • 85
  • 1
  • 9
  • Why not upload images of code https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question – Angel Cuenca Aug 18 '17 at 18:37
  • Sorry, I'll correct that! – matematika Aug 18 '17 at 18:39
  • Different variables with the same name have no connection to each other, although they may have the same value. Read about the concept of scope in your favourite book. – molbdnilo Aug 18 '17 at 18:48

4 Answers4

4

Would you create the separate class and the program itself in separate .java files or the same*?

Every class in Java should be in its own file but you can create more than one class in one file. For example

class A{
    public A(){
        System.out.println("A created");
    }
}

public class Main {
    public static void main(String[] args) {
        A x = new A();
    }
}

Can you use Scanner within the separate class, or should you use it in main() method? If so, how?

Depending on your application. Many would prefer organization and separation as much as possible so the top advice is to make it in its own class. But for very simple samples and applications I just throw it in the main() method or if I would use only once in any method. Scanner is class on its own so if you will create another class just to wrap it only alone, it really not worth it at all.

You can create Scanner as member variable of any class and use it if there are some methods you would use with it.

These just some ideas, totally depends on your design, application behavior and personal recommendation

As for how, here is an example

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number");
        int number = sc.nextInt();
        System.out.println("You entered number " + number);
    }
}

Suppose you have to use Scanner in main() method, and you declared a new variable input and initialized it as the Scanner input. How would you set that as one of the variables in the separate class? Would you make a new object and do...

Yes I would make new object of that class and initialize its members with the input value. Or i put the Scanner inside the class to ask about radius of every object created with constructor with no parameters. There are many different approaches and no right or wrong about any of them. Just avoid duplication and dependency as much as possible.

Here is your code edited with example of doing such that

import java.util.Scanner;

public class TestSimpleCircle {
    public static void main(String[] args) {

        System.out.println("Enter radius of your choice please");
        Scanner sc = new Scanner(System.in);

        SimpleCircle circle1 = new SimpleCircle(sc.nextInt()); // taking the radius as input

        System.out.println("The area of the circle of radius " + circle1.radius + " is " + circle1.getArea());

        // Create a circle with radius 25
        SimpleCircle circle2 = new SimpleCircle(25);
        System.out.println("The area of the circle of radius " + circle2.radius + " is " + circle2.getArea());

        // Create a circle with radius 125
        SimpleCircle circle3 = new SimpleCircle(125);
        System.out.println("The area of the circle of radius " + circle3.radius + " is " + circle3.getArea());

        // Modify circle radius
        circle2.radius = 100;
        System.out.println("The area of the circle of radius " + circle2.radius + " is " + circle2.getArea());

    }
}

class SimpleCircle {
    double radius;

    SimpleCircle() {
        radius = 1;
    }

    SimpleCircle(double newRadius) {
        radius = newRadius;
    }

    double getArea() {
        return radius * radius * Math.PI;
    }

    double getPerimeter() {
        return 2 * radius * Math.PI;
    }

    void setRadius(double newRadius) {
        radius = newRadius;
    }
}
Anddo
  • 2,144
  • 1
  • 14
  • 33
1

Scanner just like any other object, can be passed by reference into another method/class and still be used.

A different instance of the scanner class can be created and also used.

You would use Scanner in whichever class you needed it in. If you create a new instance of the object, it will not be connected with the other instance. This question isn't specific to Scanner, but as java Objects in general.

jthort
  • 409
  • 4
  • 14
1

For the 1st question. yes,you can create different classes in the same java file, after compilation of the java file, separate .class files will be created for each class that you have defined.

i would like to mention that you can save your java file by any name and then compile it.but if any class is declared as public (as in the above example public class filename) then the java file should have the same name as that of the public class.

On not doing so.You will get compile time error saying: class filename is public ,should be declared in a file named filename.

Hope this helps you a little...

Namrata Shukla
  • 157
  • 2
  • 8
0

One class - one file. No exception. This will save a huge time when maintain product in future.

class SimpleCircle {
    // be private - accessible only inside class
    // be final - assign only in constructor
    private final double radius;

    SimpleCircle(double newRadius) {
        radius = newRadius;
    }

    double area() {
        return radius * radius * Math.PI;
    }

    double perimeter() {
        return 2 * radius * Math.PI;
    }
}

try to avoid Constructors with useless assignments (what sense default radius=1 if you youse Constructor without parameters). Try to avoid change class state via setters. Create new objects with new state (radius).

Try to call methods nouns, that reflect the returned entity

Try to name void methods verbs, that reflect object behavior.

Now about Scanner.

try to use this idea/template

public class MyClass {
    private final Scanner scanner;

    public MyClass(Scanner scanner) {
        this.scanner = scanner;
    }

    public void any_work() {
        ...
        int readed = scanner.nextInt();
        ...
    }
}

public class Main {
    public static void main(String[] args) {
        ...
        MyClass myClass = new MyClass(new Scanner(System.in));
        ...
        myClass.any_work();
        ...
    }
}

When you create variables in a separate class, are they in any way connected to variables with the same name in the main() method or a different class?

Only one way. by method or constructor parameters. See previous code with Main/MyClass.