Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Fubarable

Thread: RomanNmeralGui && RomanNumeralList && RomanNumeralNode && RomanNumeralListIterator

  1. #1
    maria is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default RomanNmeralGui && RomanNumeralList && RomanNumeralNode && RomanNumeralListIterator

    that's what i did with my friend help so far but still am getting errors can u help me with this project and tell me what am doing wrong

    // main class
    import java.awt.*;
    import javax.swing.*;
    public class Project2 {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    int[] numbers = { 1000, 500, 100, 50, 10, 5, 1 };

    String [] Roman = { "M", "D", "C", "L", "X", "V", "I" };
    RomanNumeralList r = new RomanNumeralList();
    for (int i=0; i<Roman.length;i++){
    r.append(Roman[i]);
    }
    RNLinkedListIterator lli = r.reset();
    while (lli.hasNext())
    System.out.println(lli.next());

    JFrame romanObject = new JFrame();
    romanObject.setLocationRelativeTo(null);//lets the window popup in the
    // middle of the user's screen

    }}
    // RomanNumeral.java



    /*
    * An object of type RomanNumeral is an integer between 1 and 1000. It can
    *be constructed either from an integer or from a string that represents
    * a Roman numeral in this range. The function toString() will return a
    *standardized Roman numeral representation of the number. The function
    *toInt() will return the number as a value of type int.
    */

    public class RomanNumeral {

    private int num=0; // The number represented by this Roman numeral.

    /*
    * These array are used by the "toString()" function to construct the
    * standard Roman numeral representation of the number. For each i, the
    * number numbers[i] is represented by the corresponding string, letters[i].
    */

    private static int[] numbers = { 1000, 500, 100, 50, 10, 5, 1 };

    private static String[] Roman = { "M", "D", "C", "L", "X", "V", "I" };

    private String rn;

    public RomanNumeral(String roman){
    /** Constructor. Creates the Roman number with the given representation.
    *For example, RomanNumeral ("MCMXLII") IS 1962. If the parameter is not a
    * legal Roman numeral, a NumberFormatException is thrown. Both upper
    * and
    * lower case letters are allowed.
    */
    rn = roman;
    if (roman.length() == 0)
    throw new NumberFormatException(
    "An empty string does not define a Roman numeral.");

    // roman = roman.toUpperCase(); // Convert to upper case letters.

    int i = 0; // A position in the string, roman;
    // int arabic = 0; // Arabic numeral equivalent of the part of the string
    // that has
    // been converted so far.
    char conserletter = roman.charAt(i);
    int count = 0;
    while (i < roman.length()) {

    char letter = roman.charAt(i); // Letter at current position in
    // string.
    int number = romanToNumber(letter); // Numbers equivalent to the letters

    if (number < 0)
    throw new NumberFormatException("illegal character \"" + letter
    + "\" in roman numeral.");

    // Move on to next position in the string
    if (letter == conserletter)
    count++;
    else
    conserletter = letter;
    if (count == 5)
    throw new NumberFormatException(
    "illegal Roman Numeral. More than 3 consecutive letters.");
    i++;
    } // end while*/
    } // end constructor

    private int romanToNumber(char roman) {
    /* input an integer value of the letter considered as a Roman
    * Return
    * -1 if letter is not a legal Roman numeral. The letter must be upper
    * case.*/
    switch (roman) {
    case 'I':
    return 1;
    case 'V':
    return 5;
    case 'X':
    return 10;
    case 'L':
    return 50;
    case 'C':
    return 100;
    case 'D':
    return 500;
    case 'M':
    return 1000;
    default:
    return -1;
    }
    }

    public int getDecimalValue() {
    //return the integer value of the RomanNumeral
    int x = rn.length() - 1;
    int cha = 0;
    int i = 0;
    int valueOfPrevious = 0;
    char Roman;
    do {

    Roman = rn.charAt(x);

    switch (Roman) {
    // checking if the roman numeral is legal or illegal
    // for example, (c<M), then (CM) is 900 ,
    // because we subtract 100.
    case 'C':
    if (i % 2 == 1 && (100 < valueOfPrevious))
    cha -= 100;
    else
    cha += 100;
    valueOfPrevious = 100;
    break;

    case 'M':
    if (i % 2 == 1 && (1000 < valueOfPrevious))
    cha -= 1000;
    else
    cha += 1000;
    valueOfPrevious = 1000;
    break;
    case 'D':
    if (i % 2 == 1 && (500 < valueOfPrevious))
    cha -= 500;
    else
    cha += 500;
    valueOfPrevious = 500;
    break;
    case 'I':
    if (i % 2 == 1 && (1 < valueOfPrevious))
    cha -= 1;
    else
    cha += 1;
    valueOfPrevious = 1;
    break;
    case 'L':
    if (i % 2 == 1 && (50 < valueOfPrevious))
    cha -= 50;
    else
    cha += 50;
    valueOfPrevious = 50;
    break;
    case 'X':
    if (i % 2 == 1 && (10 < valueOfPrevious))
    cha -= 10;
    else
    cha += 10;
    valueOfPrevious = 10;
    break;
    case 'V':
    if (i % 2 == 1 && (5 < valueOfPrevious))
    cha -= 5;
    else
    cha += 5;
    valueOfPrevious = 5;
    break;
    }
    i++;
    x--;
    } while (x >= 0);
    return cha;
    }
    //s1.compareTo(s2)
    public boolean equal(Object other) {
    //to override the equals method in class Object

    return (other != null && getClass() == other.getClass() && this.rn
    .equals(((RomanNumeral) other).rn));

    }

    public int compareTo(RomanNumeral RN) {

    return this.getDecimalValue() - RN.getDecimalValue();
    }

    public String toString() {
    // Return the standard representation of this Roman numeral.
    // to override the toString method in class Object

    return rn;
    }}

    // RomanNumeralGUI.java

    import javax.swing.*;
    import java.awt.*;
    public class RomanNumeralGUI{
    static final int LIST_SIZE = 10;
    static String roman;
    static String[] romanList;
    static int romanSize;
    static TextFileInput inFile;
    static String inFileName = "Roman.txt";
    static JFrame myFrame;

    public static void main(String[] args) {
    initialize();
    readRomansFromFile(inFileName);
    printRomanList(romanList,romanSize);
    printRomantoJFrame(myFrame,romanList,romanSize);
    }
    public static void initialize() {
    roman="";
    romanList= new String[LIST_SIZE];
    romanSize=0;
    inFile = new TextFileInput(inFileName);
    myFrame=new JFrame();
    myFrame.setSize(400,200);
    myFrame.setLocation(100, 100);
    myFrame.setTitle("Roman Numerals Numbers");
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CL OSE);
    }
    public static void readRomansFromFile(String fileName){
    String roman;
    roman = inFile.readLine();
    while (roman != null) {
    assert (isValidRoman(roman)): "roman not valid";
    if (!isValidRoman(roman))
    throw new IllegalArgumentException("Invalid roman");
    else
    storeRoman(roman,romanList);
    roman = inFile.readLine();
    } //while
    } //readSSNsFromFile

    public static void storeRoman(String r, String[] list){
    assert (isValidRoman(r)): "The roman is not valid";
    assert (isValidList(list)): "The array is not valid";

    if (isValidRoman(r) && isValidList(list))
    list[romanSize++]=r;
    assert (isValidList(list)):"Resulting list not valid";
    }

    public static void printRomanList(String[] list, int size) {
    assert (isValidList(list)): "The array is not valid";
    if (!isValidList(list)){
    throw new IllegalArgumentException("Invalid list)");
    }
    for (int i=0;i<size;i++)
    if (!isValidRoman(list[i]))
    System.out.println("Invalid Roman: "+list[i]);
    else
    System.out.println(list[i]);
    }

    public static void printRomantoJFrame(JFrame jf, String[] list, int size) {
    assert (isValidList(list)): "The array is not valid";
    if (!isValidList(list)){
    throw new IllegalArgumentException("Invalid list)");
    }
    Container myContentPane = jf.getContentPane();
    TextArea myTextArea = new TextArea();
    myContentPane.add(myTextArea);

    for (int i=0;i<size;i++)
    if (!isValidRoman(list[i]))
    myTextArea.append("Invalid Roman: "+list[i]+"\n");
    else
    myTextArea.append(list[i]+"\n");
    jf.setVisible(true);
    }

    public static boolean isValidRoman(String r) {
    if (r.length() != 9) {
    throw new IllegalArgumentException("the roman is not correct ");
    }
    for (int i=0;i<9;i++)
    if (! Character.isDigit(r.charAt(i))) {
    throw new IllegalArgumentException("Roman must have digits.");
    }
    return (true);
    }
    public static boolean isValidList(String[] list){
    if (list == null){
    return false;
    }
    if (romanSize == list.length){
    return false;
    }
    return (true);
    }
    }
    // RomanNumeralList.java

    import java.util.ArrayList;
    import java.util.Hashtable;
    import java.util.Iterator;
    import java.util.List;
    import java.util.ListIterator;


    public class RomanNumeralList {

    private static final String rn = null;
    private RomanNumeralNode first;
    private RomanNumeralNode last;
    private int length;

    public RomanNumeralList () {
    RomanNumeralNode ln = new RomanNumeralNode();
    first = ln;
    last = ln;
    length = 0;
    }

    public void append (String rn) {
    RomanNumeralNode r = new RomanNumeralNode(rn);
    last.next = r;
    last = r;
    length++;
    }

    public RNLinkedListIterator reset() {
    return new RNLinkedListIterator(first.next);
    }
    public boolean equal(Object other) {
    //to override the equals method in class Object

    return (other != null && getClass() == other.getClass() && this.rn
    .equals(((RomanNumeral) other).rn));

    }
    public String toString() {
    // Return the standard representation of this Roman numeral.
    // to override the toString method in class Object

    return rn;
    }


    // public String toString() {
    // Return the standard representation of this Roman numeral.
    // to override the toString method in class Object

    // String rn;
    //return rn;
    // }

    }
    //RNLinkedListIterator.java


    public class RNLinkedListIterator {


    private RomanNumeralNode node;
    public RNLinkedListIterator(RomanNumeralNode first) {
    node = first;
    }

    public boolean hasNext() {
    return ( node != null );
    }

    public String next() {
    if ( node == null )
    throw new NullPointerException("Linked list empty.");
    String currentData = node.data;
    node = node.next;
    return currentData;
    }

    }

    //RomanNumeralNode.java

    public class RomanNumeralNode {
    String data;
    RomanNumeralNode next;

    public RomanNumeralNode(String data, RomanNumeralNode next) {
    this.data = data;
    this.next = next;
    } // constructor

    public RomanNumeralNode() {
    this.data = null;
    this.next = null;
    } // constructor

    public RomanNumeralNode(String data) {
    this.data = data;
    this.next = null;
    } // constructor

    }



    Thank You;

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: RomanNmeralGui && RomanNumeralList && RomanNumeralNode && RomanNumeralListIterato

    If you need help, then please help us help you:
    • read the forum's FAQ or my link below to see how to use code tags and add them to your post above.
    • Show us your error messages word for word.
    • Indicate which lines are causing the errors.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: RomanNmeralGui && RomanNumeralList && RomanNumeralNode && RomanNumeralListIterato

    Also, please don't delete your code like you did in your last post. Do this again and you may be banned.
    DarrylBurke likes this.

Posting Permissions

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