Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

urgent. program not oo style?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Report post to moderator
hi guys, i got a question like that:

Exercise 2 � The Copy Factory
Task: Refactor the code in exercise 2 to make better use of OO concepts and to a standard you consider acceptable for production code.

the code:

public class CopyFactory {

public CopyFactory() {
}


public Object createCopy(Object source) {
if (source instanceof Integer) {
return new Integer(((Integer)source).intValue());
}
if (source instanceof Long) {
return new Long(((Long)source).longValue());
}
if (source instanceof Float) {
return new Float(((Float)source).floatValue());
}
return null;
}

}





package com.systemsunion.precheck.exercise2;

import junit.framework.TestCase;

/**
*/
public class TestCase_CopyFactory extends TestCase {

public TestCase_CopyFactory(String name) {
super(name);
}

protected void setUp() throws Exception {
_factory = new CopyFactory();
}

protected void tearDown() throws Exception {
}

public void testInteger() throws Exception {
Integer source = new Integer(2);
Object copy = _factory.createCopy(source);
assertTrue("Wrong instance", copy instanceof Integer);
assertEquals("Should be equal", source, copy);
assertNotSame("Should be new instance", source, copy);
}

public void testLong() throws Exception {
Long source = new Long(2);
Object copy = _factory.createCopy(source);
assertTrue("Wrong instance", copy instanceof Long);
assertEquals("Should be equal", source, copy);
assertNotSame("Should be new instance", source, copy);
}

public void testFloat() throws Exception {
Float source = new Float(2.34f);
Object copy = _factory.createCopy(source);
assertTrue("Wrong instance", copy instanceof Float);
assertEquals("Should be equal", source, copy);
assertNotSame("Should be new instance", source, copy);

}

private CopyFactory _factory;
}

class TestCase_CopyFactory is a junit test, the TestCase is a included lib.

any suggestions for make the program more oo style?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please don't crosspost. I already provided an answer in the advanced forum.
 
I want my playground back. Here, I'll give you this tiny ad for it:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic