Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By SurfMan

Thread: OOP Question

  1. #1
    Join Date
    Aug 2015
    Location
    Midwest
    Posts
    3
    Rep Power
    0

    Default OOP Question

    Hello! I have a simple code, based on which I was supposed to create other classes. I did pretty much everything, except the following two lines:

    Java Code:
    myOSystem = computer.getoSystem();
    myCalculator = myOSystem.getCalculator();
    What these 2 getMethods are supposed to return? Sorry for dumb question, thanks!

    Here's the main class code:

    Java Code:
    public class SimpleCompTest {
        public static void main(String[] args) {
            //Creating a build of OS and needed software:
            Calculator calculator = new Calculator();
            FileManager fileManager = new FileManager();
    
            OSystem bestOSystem = new OSystem();
            bestOSystem.setCalculator(calculator);
            bestOSystem.setFileManager(fileManager);
    
            //Installing our build on a computer:
            Computer computer = new Computer();
            computer.setoSystem(bestOSystem);
    
            //Starting using our OS with software:
            OSystem myOSystem;
            myOSystem = computer.getoSystem();
    
            //Using calculator:
            Calculator myCalculator = new Calculator();
            myCalculator = myOSystem.getCalculator();
            int sum = myCalculator.getSum(2,2);
            myCalculator.printResult(sum);
    
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: OOP Question

    What these 2 getMethods are supposed to return?
    Look at the type of the variables to the left of the =. That is what the methods are shown to return in those two lines of code.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Aug 2015
    Location
    Midwest
    Posts
    3
    Rep Power
    0

    Default Re: OOP Question

    Quote Originally Posted by Norm View Post
    Look at the type of the variables to the left of the =. That is what the methods are shown to return in those two lines of code.
    Oh, thanks! That's what I did now:

    Java Code:
    public class Computer {
    	
    	private OSystem bestOSystem;
    
    	public OSystem getBestOSystem() {
    		return bestOSystem;
    	}
    
    	public void setBestOSystem(OSystem bestOSystem) {
    		this.bestOSystem = bestOSystem;
    	}
    }
    Last edited by Javastarter100; 08-30-2015 at 06:33 AM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: OOP Question

    Do you want to imply that every Computer instance can have its own bestOSSystem? Just asking ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    Join Date
    Aug 2015
    Location
    Midwest
    Posts
    3
    Rep Power
    0

    Default Re: OOP Question

    Quote Originally Posted by JosAH View Post
    Do you want to imply that every Computer instance can have its own bestOSSystem? Just asking ...

    kind regards,

    Jos
    I was thinking on creating 2 different builds of OS: one with file manager, and another with calculator:
    Java Code:
    OSystem build1 = new OSystem();
    build1.setFileManager(fileManager);
            
    OSystem build2 = new OSystem();
    build2.setMyCalculator(myCalculator);
    ... and installing them on two different computers:
    Java Code:
     Computer computer1 = new Computer();
     computer1.setBestOSystem(build1);
    
     Computer computer2 = new Computer();
     computer2.setBestOSystem(build2);
    Is this correct OOP? Thanks!

  6. #6
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Location
    Planet Earth
    Posts
    278
    Rep Power
    0

    Default Re: OOP Question

    I think you should make OSystem, a parent class and then extend it to create different builds of OS. Like

    OSystem build1 = new FileMangerOS();
    OSystem build2 = new CalculatorOS();
    Last edited by allaudin; 08-30-2015 at 09:55 PM.

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: OOP Question

    Quote Originally Posted by allaudin View Post
    I think you should make OSystem, a parent class and then extend its functionality to create different builds of OS. Like

    OSystem build1 = new FileMangerOS();
    OSystem build2 = new CalculatorOS();
    So what happens when you need an OS that has a FileManager AND a Calculator? Would you be making: CalculatorOS that extends FileManagerOS that extends OSystem? That will become messy very quickly. You might need a different approach. There are lots of ways to do this. Maybe an interface can help you out:

    Java Code:
    interface CalculatorSupplier {
        public Calculator getCalculator();
    }
    
    interface FileManagerSupplier {
        public FileManager getFileManager();
    }
    
    abstract class OperatingSystem {
        public abstract String getName();
    }
    
    class WindowsOperatingSystem extends OperatingSystem implements CalculatorSupplier, FileManagerSupplier {
        public String getName() {
            return "Windows";
        }
    
        public Calculator getCalculator() {
            return new Calc();
        }
    
        public FileManager getFileManager() {
             return new Explorer();
        }
    }
    
    
    class LinuxOperatingSystem extends OperatingSystem implements CalculatorSupplier, FileManagerSupplier {
        public String getName() {
            return "Linux";
        }
    
        public Calculator getCalculator() {
            return new LinuxCalculator();
        }
    
        public FileManager getFileManager() {
             return new LinuxFileManager();
        }
    }
    Calculator and FileManager themselves are interfaces, and Calc, Explorer, LinuxCalculator and LinuxFileManager are classes, implementations of these interfaces. Each have their OS specific implementation.

    Every time you need to add functionality to your OS, you can append the interface to the class definition and then the OS class can do its platform specifc thing, without having to rewrite a load of subclasses.
    allaudin likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Location
    Planet Earth
    Posts
    278
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 05-25-2012, 04:00 PM
  2. Java Question [Beginner Question]
    By joker760 in forum New To Java
    Replies: 3
    Last Post: 12-13-2011, 04:01 PM
  3. question posted by indissa: library question.
    By Fubarable in forum New To Java
    Replies: 2
    Last Post: 11-18-2011, 01:14 AM
  4. Question concerning question marks and colons
    By jim01 in forum New To Java
    Replies: 17
    Last Post: 01-14-2011, 12:05 AM
  5. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •