# MyInteger Class

Page 1 of 1

## 4 Replies - 18835 Views - Last Post: 05 September 2010 - 07:44 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=189144&amp;s=85b8b3457f7ef1e55e8344ff5d5e0d9b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 2
• Posts: 59
• Joined: 05-September 10

# MyInteger Class

Posted 05 September 2010 - 06:43 AM

Hello everyone,

I am having trouble finishing my program. I am having the most trouble with the methods, most of them are in need of fixing but one is giving me the most trouble. As described in the problem description, parseInt(char[]). I can't figure out what to do, I don't know how to properly write the method for this.

I will include the entire program description for the perusal of anyone interested:

Problem Description:
Design a class named MyInteger. The class contains:
An int data field named value that stores the int value represented by this object.
A constructor that creates a MyInteger object for the specified int value.
A get method that returns the int value.
Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively.
Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively.
Static methods isEven(MyInteger), isOdd(MyInteger), and isPrime(MyInteger) that return true if the specified value is even, odd, or prime, respectively.
Methods equals(int) and equals(MyInteger) that return true if the value in the object is equal to the specified value.
A static method parseInt(char[]) that converts an array of numeric characters to an int value.
A static method parseInt(String)that converts a string into an int value.

```public class NewMyInteger {

public static void main(String[] args) {
MyInteger n1 = new MyInteger(5);
System.out.println("n1 is even? " + n1.isEven());
System.out.println("n1 is prime? " + n1.isPrime());
System.out.println("15 is prime? " + MyInteger.isPrime(15));

char[] chars = {'3', '5', '3', '9'};
System.out.println(MyInteger.parseInt(chars));

String s = "3539";
System.out.println(MyInteger.parseInt(s));

MyInteger n2 = new MyInteger(24);
System.out.println("n2 is odd? " + n2.isOdd());
System.out.println("45 is odd? " + MyInteger.isOdd(45));
System.out.println("n1 is equal to n2? " + n1.equals(n2));
System.out.println("n1 is equal to 5? " + n1.equals(5));
}
}

class MyInteger {

//int data field
int value;

//constructor that creates a MyInteger object for specified int value
public MyInteger(int value) {
this.value = value;
}

//get method to return the int value
public Integer getValue() {
return value;
}

//method for returning boolean value for isEven
public boolean isEven() {
if (value % 2 == 0) {
return true;
}
return false;
}

//method for returning boolean value for isOdd
public boolean isOdd() {
if (value % 2 != 0) {
return true;
}
return false;
}

// method for returning boolean value isPrime
public boolean isPrime() {
int factor = 0;
for (factor = 2; factor <= value / 2; factor++) {
if (value % factor != 0) {
return true;
}
}
return false;
}

public boolean isEven(int value) {
if (value % 2 == 0) {
return true;
}
return false;
}

public boolean isOdd(int value) {
if (value % 2 != 0) {
return true;
}
return false;

}

public static boolean isPrime(int value) {
return isPrime(value);
}

public static boolean isEven(MyInt myInt) {
return myInt.isEven(myInt.getValue());
}

public static boolean isOdd(MyInt myInt) {
return myInt.isOdd(myInt.getValue());
}

public static boolean isPrime(MyInt MyInt) {
return MyInt.isPrime(MyInt.getValue());
}

public boolean equals(int intValue) {
return value == intValue;
}

public boolean equals(MyInt myInt) {
return equals(myInt.getValue());
}

public static int parseInt(String s) {
return Integer.parseInt(s);
}

public static int parseInt() {

}

}
```

This post has been edited by xterradaniel: 05 September 2010 - 06:44 AM

Is This A Good Question/Topic? 0

## Replies To: MyInteger Class

### #2 ipushmycar

• D.I.C Regular

Reputation: 86
• Posts: 390
• Joined: 29-August 10

## Re: MyInteger Class

Posted 05 September 2010 - 07:08 AM

Well first you need some better variable names... you have my value as an instance variable then again as parameters, I would change one.

1. For converting the array of Chars[] to integer you need to pass the method one first.
2. Then I would go through the array and use Character.forDigit(array[x],10). Look it up in the API to further understand it. It basiclly converts a Character (0-9) to a number.
OR
1. You could just go through the array of char and add them to string to make a string of numbers.
2. Then just call your public static int parseInt(String s).

There may be a better way to do this but both of my ways would work, it also depends on how your professor wanted you to do it.

Good luck hope this helped.

### #3 ThePheonix21

Reputation: 0
• Posts: 54
• Joined: 25-November 09

## Re: MyInteger Class

Posted 05 September 2010 - 07:16 AM

``` char[] chars = {'3', '5', '3', '9'};
System.out.println(MyInteger.parseInt(chars));

```

There's part of your problem, you are telling the method MyInteger to turn those characters into integers, it can't do that. Try this instead:
``` System.out.println(MyInteger(Integer.parseInt(chars));
```

Reputation: 2
• Posts: 59
• Joined: 05-September 10

## Re: MyInteger Class

Posted 05 September 2010 - 07:24 AM

I got it to work. If you don't mind could you see if any of my code can be improved or does it look good. I rewrote the entire thing.

Thanks again for all your help!

```/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Becky and Daniel's
*/
public class TestMyInteger {

public static void main(String[] args) {
MyInteger n1 = new MyInteger(5);
System.out.println("n1 is even? " + n1.isEven());
System.out.println("n1 is prime? " + n1.isPrime());
System.out.println("15 is prime? " + MyInteger.isPrime(15));

char[] chars = {'3', '5', '3', '9'};
System.out.println(MyInteger.parseInt(chars));

String s = "3539";
System.out.println(MyInteger.parseInt(s));

MyInteger n2 = new MyInteger(24);
System.out.println("n2 is odd? " + n2.isOdd());
System.out.println("45 is odd? " + MyInteger.isOdd(45));
System.out.println("n1 is equal to n2? " + n1.equals(n2));
System.out.println("n1 is equal to 5? " + n1.equals(5));
}
}

class MyInteger {

int value;

MyInteger(int newValue) {
value = newValue;
}

public int getValue() {
return value;
}

public static boolean isEven(int n) {
return (n % 2 == 0);
}

public static boolean isOdd(int n) {
return !isEven(n);
}

public static boolean isPrime(int n) {
for (int f = 2; f < n / 2; f++) {
if (n % f == 0) {
return false;
}
}
return true;
}

public static boolean isEven(MyInteger n) {
return n.isEven();
}

public static boolean isOdd(MyInteger n) {
return n.isOdd();
}

public static boolean isPrime(MyInteger n) {
return n.isPrime();
}

public boolean isEven() {
return isEven(value);
}

public boolean isOdd() {
return isOdd(value);
}

public boolean isPrime() {
return isPrime(value);
}

public boolean equals(int n) {
return (value == n);
}

public boolean equals(MyInteger n) {
return equals(n.getValue());
}

public static int parseInt(String s) {
return Integer.parseInt(s);
}

public static int parseInt(char[] s) {
return parseInt(new String(s));
}
}

```

### #5 ThePheonix21

Reputation: 0
• Posts: 54
• Joined: 25-November 09

## Re: MyInteger Class

Posted 05 September 2010 - 07:44 AM

```char[] chars = {'3', '5', '3', '9'};

System.out.println(MyInteger.parseInt(chars));

String s = "3539";

System.out.println(MyInteger.parseInt(s));
```

Why do you have a character array of integers? It almost seems like it's superfluous to turn them into characters, only turn them back to integers to be printed. Same with the string, why make an integer into a string when you can just declare and initialize it as an integer? Overall the code works, but a couple other things you need to work on, 1)as everyone else said, you need to give your variables more descriptive names and 2)you need to comment your code so when you ask for help, we can figure out what each line should do faster.