Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Simple java application

  • 01-03-2004 10:24PM
    #1
    Closed Accounts Posts: 38


    Any one want to try their hand at this?

    Create a console based app (runs through the command prompt) called Abacus.
    This will just have a main() Method.
    Create a class called Calculator.
    This will have 4 methods called Add() Multiply () Divide() Subtract().
    These methods will take two integer values as parameters and return an integer value.
    Instantiate the Calculate class and execute the 4 methods using default values "2" and "4" and print out the results to the screen.


Comments

  • Registered Users, Registered Users 2 Posts: 5,333 ✭✭✭Cake Fiend


    I don't even know Java and I reckon I could have a decent stab at this.
    Make some effort.


  • Closed Accounts Posts: 302 ✭✭Auburn


    Do your own homework or at least show what you've tried. It isn't hard even if you are a complete beginner


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Jeezus you could at least try and dress up the question so that it doesn't look too much like homework.
    Kids these days.... pah!


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Not sure it if compiles. Might need 'tweaking' but your instructor will love it.
    Class Abacus {
    	int i1 = 0, i2 = 2;
    	public static void main(String[] args) {
    		calculator[] s[] = new calculator[i1][i2];
    		if ( i1 > i2) {
    			main(s[0][0]);
    		} else if ( i2 < i1) {
    			main(s[0][1]);
    		} else 	main(s[0][0]);
    	}
    	void main(calculator c) {
    		c.main();
    	}
    }
    Class Calculator extends calculator {
    	public int i1 = 0 , i2 = i1.clone();
    	
    	void main() {
    		Class calculator = Calculator.class;
    		Calculator c = new (Calculator) Calculator();
    		System.out.println("Add:" + Add(2,4));
    		System.out.println("Multiply:" + Multiply(2,4));
    		System.out.println("Subtract:" + Subtract(2,4));
    		System.out.println("Divide:" + Subtract(4,2));
    	}
    
    	static String Add(int i1, int i2) {
    		do { 
    			return "0";
    		} while (i1 < i2);
    	}
    
    	static String Multiply(int i1, int i2) {
    		return (Add(i1,i2) == add(i1,i2)) ? Add(i1,i2) : add(i1,i2);
    	}
    	
    	static String add(int i2, int i1) {
    		Subtract(i2,i1);
    	}
    
    	static int Divide(int i1, int i2) {
    		try {
    			return this.i1 / this.i2;
    		} catch (Exception e) {
    			return 0
    		}
    	}
    	static String Subtract(int i1, int i2) {
    		return (i1 == i2) ? this.i2 - this.i1 : i1 - i2;
            }
    }
    Class calculator {
    	void main() {
    	}
    }
    


  • Moderators, Society & Culture Moderators Posts: 2,687 Mod ✭✭✭✭Morpheus


    I think people should be banned from doing others college work.

    When i was in college, at least we bothered to search for the code on the internet!!

    Lazy feckers.


  • Advertisement
  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Good one Hobbes :)


  • Registered Users, Registered Users 2 Posts: 821 ✭✭✭Dr Pepper


    Yeh, nicely done..


  • Registered Users, Registered Users 2 Posts: 3,548 ✭✭✭Draupnir


    Hey Hobbes just wondering a few things:

    1. Why did you put a try catch block in the divide method? Any reason or just for the laugh?

    2. Whats with the 2 calculator classes, well Calculator and calculator?

    Also, I dont think its very nice of the thread starter to go asking people to do their college work for them, especially when its so damn simple


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    1) Ever try to divide by zero in Java?

    2) I'm guessing here, but I'd say just cause :)


  • Registered Users, Registered Users 2 Posts: 16,415 ✭✭✭✭Trojan


    Don't be such a lazy twat. Closed.


  • Advertisement
This discussion has been closed.
Advertisement