Simple question

im having a hard time with constructors... i keep declaring a method instead of a constructor..so basically, is the constructor supposed have a separate class? and is it supposed to be outside the main method?thanks in advance-Arch-^_^
[263 byte] By [Arch_Bytesa] at [2007-11-26 19:15:56]
# 1
Since you haven't posted code, it's hard to say what you're doing wrong.Be brave, post your attempt and we'll laugh^H^H^H^H^H correct them.
DrLaszloJamfa at 2007-7-9 21:28:20 > top of Java-index,Java Essentials,New To Java...
# 2

oopss.. sorry..^_^ this problem given to me requires me to have a constructor.. i know i could finish it without a construction.. but i must follow the rules.. anyway here it is.. its still incomplete though

import java.util.*;

public class ConvertRoman {

public static void main(String args[])throws Exception{

HashMap values = new HashMap();

values.put(new Character('M'), new Integer(1000));

values.put(new Character('D'), new Integer(500));

values.put(new Character('C'), new Integer(100));

values.put(new Character('L'), new Integer(50));

values.put(new Character('X'), new Integer(10));

values.put(new Character('V'), new Integer(5));

values.put(new Character('I'), new Integer(1));

Scanner kybd = new Scanner(System.in);

String number;

System.out.println("Input Roman or Arabic number to convert:");

System.out.println();

number = kybd.nextLine();

number = number.toUpperCase();

for(int x = 0; x < number.length();x++){

if((int)number.charAt(x) > 47 && (int)number.charAt(x)<58) {

RomanNum passval = new RomanNum(Integer.parseInt(number));

}

{

}

}

}

class RomanNum {

public RomanNum(int convert){

try

{if(convert > 3999 && convert < 1)

{

throw new NumberFormatException();

}

}catch(NumberFormatException a){

System.out.println("Integer value to big to convert to Roman Numeral!");

}

}

}

forgive my incompetence.. ^_^

Arch_Bytesa at 2007-7-9 21:28:20 > top of Java-index,Java Essentials,New To Java...
# 3

the constructor is not a separate class.....and yes it is outside the main method...it is generally used to pass various arguments while initializing the class.......

as an example...

class Rectangle{

public Rectangle(int a){

}

public Rectangle(int a,String d){

}

public.....main....

{

///so now u can initialize in three ways......

Rectangle rect=new Rectangle();//this is default of course

Rectangle rect=new Rectangle(15);

Rectangle rect=new Rectangle(15,s);

}

}

sweta_da at 2007-7-9 21:28:20 > top of Java-index,Java Essentials,New To Java...
# 4
ok.. i got that part.. am i allowed to do things inside the constructor? like example a try catch statement?
Arch_Bytesa at 2007-7-9 21:28:20 > top of Java-index,Java Essentials,New To Java...
# 5

You *can* put most anything in the constructor, but in this case it's wrong.

If a constructor completes normally, the resulting object should be in

a sensible state. But if I execute new RomanNum(12345), there's some

warning printed on the console, but the code continues normally.

That's a dsign error. Constructors are allowed to throw exceptions (hint).

DrLaszloJamfa at 2007-7-9 21:28:21 > top of Java-index,Java Essentials,New To Java...
# 6
oh.. okay i see.. thanks..^_^
Arch_Bytesa at 2007-7-9 21:28:21 > top of Java-index,Java Essentials,New To Java...
# 7
i've just been able to create and initialize a constructor.. ^_^ is it possible to create a method inside a try/catch statement that's in a constructor?
Arch_Bytesa at 2007-7-9 21:28:21 > top of Java-index,Java Essentials,New To Java...
# 8

public class Construct {

public Construct() {

try {

public void method() {

// code

}

} catch (Exception e) {

// TODO: handle exception

}

}

}

like that? no. you can't create methods inside a method or a constructor.

albertsesea at 2007-7-9 21:28:21 > top of Java-index,Java Essentials,New To Java...
# 9
ah.. i see.. ok thanks.. i guess ill just call the method from the constructor then.. thanks^_^
Arch_Bytesa at 2007-7-9 21:28:21 > top of Java-index,Java Essentials,New To Java...
# 10
yep, you do that =)
albertsesea at 2007-7-9 21:28:21 > top of Java-index,Java Essentials,New To Java...
# 11

i've gotten through the constructors and methods..

now, i am wondering if there is a way to find locate the key for a given HashMap value..

i used a HashMap for my conversion of Roman Numerals to Arabic

and vice versa.. but it seems my HashMap can only do Roman to Arabic..

HashMap values = new HashMap();

values.put(new Character('M'), new Integer(1000));

values.put(new Character('D'), new Integer(500));

values.put(new Character('C'), new Integer(100));

values.put(new Character('L'), new Integer(50));

values.put(new Character('X'), new Integer(10));

values.put(new Character('V'), new Integer(5));

values.put(new Character('I'), new Integer(1));

is there a way for me to get the KEY by giving the VALUE?

Arch_Bytesa at 2007-7-9 21:28:21 > top of Java-index,Java Essentials,New To Java...
# 12
Loop.And, as a friendly reminder for those that already answered your original question, don't forget to distribute your Dukes to them (some people in this forum, AbiSS I'm looking your way, are very sensitive about this).
masijade.a at 2007-7-9 21:28:21 > top of Java-index,Java Essentials,New To Java...
# 13
dont worry i have.. ^_^ well, anyway i used an if statement instead for the Conversion of Arabic to Roman.. ill use the HashMap for Roman to Arabic.. thanks..^_^
Arch_Bytesa at 2007-7-9 21:28:21 > top of Java-index,Java Essentials,New To Java...
# 14

this has stomped me..

i am already able to convert Arabic Numbers to Roman numerals.. but im having hard time converting Roman Numerals to Arabic..

this is what ive got so far

static int toInteger(String roman){//my method that does the converting

int NewValue = 0;

HashMap values = new HashMap();

values.put(new Character('M'), new Integer(1000));

values.put(new Character('D'), new Integer(500));

values.put(new Character('C'), new Integer(100));

values.put(new Character('L'), new Integer(50));

values.put(new Character('X'), new Integer(10));

values.put(new Character('V'), new Integer(5));

values.put(new Character('I'), new Integer(1));

for(int x = 0; x < roman.length(); x++){

if(values.containsKey((Character)roman.charAt(x))) { //this is where it fails..

//it says that it cannot cast from char to Character..

//but the containsKey function needs

//an object parameter.. how do i go about that?

}

}

return NewValue;

}

thanks in advance^_^

Arch_Bytesa at 2007-7-9 21:28:21 > top of Java-index,Java Essentials,New To Java...
# 15
Changeif (values.containsKey((Character)roman.charAt(x))) {toif (values.containsKey(new Character(roman.charAt(x))))
masijade.a at 2007-7-21 17:32:12 > top of Java-index,Java Essentials,New To Java...
# 16
thank you!!! ^_^ btw.. ive finished all my duke stars and i want to award you some.. so how should i add more duke stars to this thread?
Arch_Bytesa at 2007-7-21 17:32:12 > top of Java-index,Java Essentials,New To Java...
# 17
Don't know, have never actually asked a question here. But don't worry about it. I don't care about the things. The thank you is enough.
masijade.a at 2007-7-21 17:32:12 > top of Java-index,Java Essentials,New To Java...