Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
martin li
1,026 PointsSchool Homework URGENT help!!
I am lost in classes and its due 11:00pm EST so I need helpgetting this done can anybody please help me get through this. I am completely lost :S
- (MyInteger class) - Design a class named MyInteger. The class contains: o An int data field named value that stores the int value represented by this object. o A constructor that creates a MyInteger object for the specified int value. o A get method that returns the int value. o Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. o Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. o Static methods isEven(MyInteger), isOdd(MyInteger), and isPrime(MyInteger) that return true if the specified value is even, odd, or prime, respectively. o Methods equals(int) and equals(MyInteger) that return true if the value in the object is equal to the specified value. o A static method parseInt(String) that converts a string to an int value. Implement the method without using Integer.parseInt(x).
Implement the class. Write a client program that tests all methods in the class.
(The MyPoint class) - Design a class named MyPoint to represent a point with x and y coordinates. The class contains: o Two data fields x and y that represent the coordinates. o A no-arg constructor that creates a point (0, 0). o A constructor that constructs a point with specified coordinates. o Two get methods for data fields x and y, respectively. o A method named distance that returns the distance from this point to another point of the MyPoint type. o A method named distance that returns the distance from this point to another point with specified x and y-coordinates. o Implement the class. Write a test program that creates two points (0, 0) and (10, 30.5) and displays the distance between them.
(Tokenization) - Tokenization is the process of breaking a stream of text up into words, phrases, symbols, or other meaningful elements called tokens. Write a program that reads a text file passes as an argument to the main method from the command line and extracts all the unique words from the file and prints them in the console one per line. You should discard any other characters in the file: white spaces, digits and punctuation characters.
(Reformatting Java source code) - Write a program that converts the Java source code from the next-line brace style to the end-of-line brace style. For example, the following Java source uses the next-line brace style. Your program converts it to the end-of-line brace style. Your program can be invoked from the command line with the Java source code file as the argument. It converts the Java source code to a new format. For example, the following command converts the Java source code file Test.java to the end-of-line brace style.
java Reformat Test.java
where Test.java was:
public class Test { public static void main(String[] args) { System.out.println("Test 1"); System.out.println("Test 2"); } }
Output Test.java:
public class Test{ public static void main(String[] args){ System.out.println("Test 1"); System.out.println("Test 2"); } }
Notes: In the "Exceptions and IO" lecture notes, there are 3 classes that you can use: o java.io.File: you just have to create a file with: File f = new File("Test.java"); o java.util.Scanner: you have to read from the file: Scanner s = new Scanner(f); while(s.hasNext()) you can read with s.nextLine(); you have to close the Scanner with s.close() o java.io.PrintWriter: you have to write the file: PrintWriter p = new PrintWriter(f); p.print("..."); you have to close the PrintWriter with p.close() You can find code examples in the lecture notes.
Please submit only the Java files and include your name and ID in each Java file's first line as a line comment - for example: “// My name, 1234567890.”
Each problem is 3 points and the total is 12 points.
2 Answers
miguelcastro2
Courses Plus Student 6,573 PointsWriting code for you is not going to help you. Especially if you want to learn this. Try to write some of the code and once you get at a point where you need help then ask for it, otherwise, don't have others try to complete your homework for you.
Devin Scheu
66,191 PointsHere's your first one, i'm not doing the other ones for you:
public class MyInteger {
public int mValue = 23;
private int mMyInt;
public static void main(String[] args) {
}
public MyInteger(int x) {
mMyInt = x;
}
public int getValue() {
return mValue;
}
public static boolean isEven(int x) {
if ( (x & 1) == 0 ) {
return true;
} else {
return false;
}
}
public static boolean isOdd(int x) {
if (x % 2 == 0) {
return true;
} else {
return false;
}
}
public static boolean isPrime(int n) {
if (n%2==0) return false;
for(int i=3;i*i<=n;i+=2) {
if(n%i==0)
return false;
}
return true;
}
public boolean equalsInt(int x) {
if (x == mValue) {
return true;
} else {
return false;
}
}
public boolean equalsMyInt(int x) {
if (x == mMyInt) {
return true;
} else {
return false;
}
}
public int parseInt(String x) {
int stringToInt = Integer.parseInt(x);
return stringToInt;
}
}
martin li
1,026 Points@Devin_schecu thank you but heres my attempt but i got stuck :/
public class MyInteger { private static int value; public int num = ; public MyInteger(int value){ this.value = value; }
public int getValue(){
return value;
}
public static boolean isEven() {
if (value%2==0)
return true;
else
return false;
}
public static boolean isOdd(){
if(value%2!=0)
return true;
else
return false;
}
public static boolean isPrime(){
for(int i =2;i<value;i++){
if(value%i==0){
return false;
}
else
return true;
}
}
public boolean equals(int value){
return this.value == value;
}
public boolean equals(MyInteger value){
re
}
}