PDA

View Full Version : help w/ program


katCT
11-07-2005, 09:18 PM
i have an assignment due tuesday i am getting some errors when i go to compile it, I cant figure out whats wrong. It says something about the class
Can anyone help?


import java.util.StringTokenizer;

public class phone_Tokens {

public static void main (String[] args) {

phone_Tokenizer phone = new phone_Tokenizer ("(nnn)-nnn-nnnn");

System.out.println ("Areacode: " + phone.get_areacode());
System.out.println ("Exchange: " + phone.get_exchange());
System.out.println ("Extension: " + phone.get_extension());
}
}

class phone_Tokenizer {
private String areacode;
private String exchange;
private String extension;

public phone_Tokenizer (String Phone_number) {

StringTokenizer phone = new StringTokenizer (phone_number, ":");
areacode = phone.nextToken(":/");
exchange = phone.nextToken();
extension = phone.nextToken();
}

public String get_areacode () {
return areacode;
}

TheShaner
11-08-2005, 03:42 PM
You're making your program needlessly complicated. You have a new thread that describes an error about your class being in a separate file. Well, the way you have it set up, that is exactly what you need to do to the phone tokenizer class. But I would rather set up your program differently. And also, please do not keep creating new threads. Stick with your original. You can add a reply to it if you really feel it needs to be bumped up in the list.

import java.util.StringTokenizer;

public class phone_Tokens {

public static void main (String[] args) {

String areacode, exchange, extension;

// Creates tokens that are delimited by a dash, thus
// (nnn) is one token, nnn is the next, and the last is nnnn
StringTokenizer phone = new StringTokenizer ("(nnn)-nnn-nnnn", "-");

areacode = phone.nextToken();
exchange = phone.nextToken();
extension = phone.nextToken();

System.out.println ("Areacode: " + areacode);
System.out.println ("Exchange: " + exchange);
System.out.println ("Extension: " + extension);
}
}
If you only need the three numbers inside the () for the area code, then see if you can figure out how to use an available String method to do so. You would use the method just on the areacode variable and not the entire phone number.

-Shane

katCT
11-08-2005, 04:08 PM
Sorry about that I'm new to this.
I have another assignment I am also struggling with it is
Write a class called Triangle that can be used to represent a triangle.
It should include the following methods that return boolean
values indicating if the particular property holds:
- isRight (a right triangle)
- isScalene (no two sides are the same length)
- isIsosceles (exactly two sides are the same length)
- isEquilateral (all three sides are the same length).

Write an application class to test the triangle class.

I am very bad at this stuff. All I have so far is:
class Triangle {
private final int MAX_LEVEL = 100;
private final int MIN_LEVEL = 0
private int sd1, sd2, sd3;

public Triangle (int side1, int side2, int side3) {
sd1 = side1;
sd2 = side2;
sd3 = side3;
}

If someone could maybe point me in the right direction I can probably figure it out.
Thanks

TheShaner
11-08-2005, 04:50 PM
That's ok :) Just stick with one thread for one topic. If you have a new topic, like a new assignment, you can post a new thread. So you could've made this assignment a new thread, but since you've started here, we'll continue here. Also, did your phone number assignment work? Do you understand what that is doing now?

For your triangle class, you're on the right path. Now you need to start making methods called isRight, isScalene, isIsosceles, and isEquilateral which will return true or false based on if they meet the conditions. I'll give you one of the methods and you go from there:
public boolean isEquilateral()
{
if ((sd1 == sd2) && (sd2 == sd3))
return true;
else
return false;
}
Also, take a look at this post that someone made here: http://codingforums.com/showthread.php?t=71277

Your Triangle class will be a separate java file. You will then make another java file that will just contain the main class that will demonstrate using the Triangle class. This second file will import your Triangle class so that you can do something like this (this is if you name your class for your triangle: Triangle):
import Triangle;

public class demonstrate_Triangle {

public static void main (String[] args) {

Triangle myTriangle = new Triangle(5,5,5);

if (myTriangle.isEquilateral())
System.out.println ("This is an equilateral triangle.");
else
System.out.println ("This is NOT an equilateral triangle.");

}
}
-Shane

katCT
11-08-2005, 05:13 PM
yes I did get the last problem to work. How do i post it so its gray like that instead of just copying and pasting?

here is what I have added:

private final int MAX_LEVEL = 100;
private final int MIN_LEVEL = 0
private int sd1, sd2, sd3;

public Triangle (int side1, int side2, int side3) {
sd1 = side1;
sd2 = side2;
sd3 = side3;
}

public boolean isEquilateral()
{
if ((sd1 == sd2) && (sd2 == sd3))
return true;
else
return false;
}

public boolean isRight()
{

if ((sd1 * sd1) + (sd2 * sd2) == (sd3 * sd3))
return true;
else
return false;

public boolean isScalene()
{
if ((sd1 != sd2) && (sd1 != sd3) && (sd2 != sd3))
return true;
else
return false;


I also don't really understand the second part about a second class.
Thank you so much for helping me.

Kat

TheShaner
11-08-2005, 05:34 PM
First, whatever code you write, highlight it all and then select the pound sign (#) button that is near all the other formatting buttons. That will enclose your code in CODE tags. This way, if you have a lot of code, it will keep it in a fixed height and width box that will have scrollbars and will keep your code much more readable. That being said, let's get your program working.

If you don't understand the concept of having two files with one file importing the other, then let's skip that. I think i'm making it needlessly complicated this time, hehe. All you have to do is within your Triangle class, add your main class like so:

class Triangle {

private final int MAX_LEVEL = 100; // what is this being used for?
private final int MIN_LEVEL = 0; // same with this
private int sd1, sd2, sd3;

public Triangle (int side1, int side2, int side3) {
sd1 = side1;
sd2 = side2;
sd3 = side3;
}

public boolean isEquilateral() {
if ((sd1 == sd2) && (sd2 == sd3))
return true;
else
return false;
}

public boolean isRight() {
if ((sd1 * sd1) + (sd2 * sd2) == (sd3 * sd3))
return true;
else
return false;
}

public boolean isScalene()
{
if ((sd1 != sd2) && (sd1 != sd3) && (sd2 != sd3))
return true;
else
return false;
}

// Add isIsosceles method here

public static void main (String[] args) {

Triangle myTriangle = new Triangle(5,5,5);

if (myTriangle.isEquilateral())
System.out.println ("This is an equilateral triangle.");
else
System.out.println ("This is NOT an equilateral triangle.");

// Test out your other methods here

}
}
You should be able to finish the rest now :thumbsup:

-Shane

katCT
11-08-2005, 05:59 PM
I think I only have one last question it is w/ the isosceles triangle. I am a little confused with the if and else statements I tried it but I'm pretty sure it's not right.

public boolean isIsosceles()
{
if ((sd1 == sd2) && (sd1 != sd3))
return true;
if ((sd2 == sd3) && (sd1 != sd2))
return true;
if ((sd1 == sd3) && (sd1 != sd2))
return true;
else
return false;
}

TheShaner
11-08-2005, 06:16 PM
public boolean isIsosceles()
{
if ((sd1 == sd2) && (sd1 != sd3))
return true;
if ((sd2 == sd3) && (sd1 != sd2))
return true;
if ((sd1 == sd3) && (sd1 != sd2))
return true;
else
return false;
}

Well, that should actually work just because you return if it is true before it gets to go on to check the other ifs, but it's better if you set it up like the below because it makes more logical sense and prevents any locgial errors:
public boolean isIsosceles() {
if ((sd1 == sd2) && (sd1 != sd3))
return true;
else if ((sd2 == sd3) && (sd1 != sd2))
return true;
else if ((sd1 == sd3) && (sd1 != sd2))
return true;
else
return false;
}

Or think about this:

A triangle is an Isosceles triangle if it is not Equilateral and it is not Scalene. You could do this instead:

public boolean isIsosceles() {
if (this.isEquilateral() || this.isScalene())
return false;
else
return true;
}
Although now I may have confused you with the this operator. All that operator does is use the current object, which is your Triangle object you created in your main method. The above just checks to see if your triangle is equilateral or scalene. If it is, then it's not an isosceles. If not, then it is.

-Shane

katCT
11-08-2005, 06:56 PM
class Triangle {

private int sd1, sd2, sd3;

public Triangle (int side1, int side2, int side3) {
sd1 = side1;
sd2 = side2;
sd3 = side3;
}

public boolean isEquilateral()
{
if ((sd1 == sd2) && (sd2 == sd3))
return true;
else
return false;
}

public boolean isRight()
{

if ((sd1 * sd1) + (sd2 * sd2) == (sd3 * sd3))
return true;
else
return false;
}

public boolean isScalene()
{
if ((sd1 != sd2) && (sd1 != sd3) && (sd2 != sd3))
return true;
else
return false;
}
public boolean isIsosceles() {
if ((sd1 == sd2) && (sd1 != sd3))
return true;
else if ((sd2 == sd3) && (sd1 != sd2))
return true;
else if ((sd1 == sd3) && (sd1 != sd2))
return true;
else
return false;
}
public static void main (String[] args) {

Triangle myTriangle = new Triangle(5,5,5);

if (myTriangle.isEquilateral())
System.out.println ("This is an equilateral triangle.");
else
System.out.println ("This is NOT an equilateral triangle.");
}
pubic static void main (String[] args) {
Triangle myTriangle = new Triangle(5,3,6);

if (my Triangle. isEquilateral())
System.out.println ("This is an equilateral triangle.");
else
System.out.println ("This is not an equilateral triangle.");
}
public static void main (String[] args) {

Triangle myTriangle = new Triangle(3,4,5);

if (myTriangle.isRight())
System.out.println ("This is a right triangle.");
else
System.out.println ("This is NOT a right triangle.");
}
public static void main (String[] args) {

Triangle myTriangle = new Triangle(3,5,6);

if (myTriangle.isRight())
System.out.println ("This is a right triangle.");
else
System.out.println ("This is NOT a right triangle.");
}
public static void main (String[] args) {

Triangle myTriangle = new Triangle(3,4,5);

if (myTriangle.isIsosceles())
System.out.println ("This is an Isosceles triangle.");
else
System.out.println ("This is NOT an Isosceles triangle.");
}
public static void main (String[] args) {

Triangle myTriangle = new Triangle(3,3,5);

if (myTriangle.isIsosceles())
System.out.println ("This is an Isosceles triangle.");
else
System.out.println ("This is NOT an Isosceles triangle.");
}
public static void main (String[] args) {

Triangle myTriangle = new Triangle(3,4,5);

if (myTriangle.isScalene())
System.out.println ("This is an scalene triangle.");
else
System.out.println ("This is NOT an scalene triangle.");
}
public static void main (String[] args) {

Triangle myTriangle = new Triangle(3,3,5);

if (myTriangle.isScalene())
System.out.println ("This is an Scalene triangle.");
else
System.out.println ("This is NOT an Scalene triangle.");
}
}


I couldnt get the grey box thing to work. I'm getting two errors, one says identifier expected public static void
and ; expected.

TheShaner
11-08-2005, 08:13 PM
Well first, you can only have one main method. You have a bunch in there, hehe. Second, you're declaring Triangles with the same name. With each new Triangle, use a different name. Third, you're creating Triangles that have the same sides. No need to do that if you have a Triangle with those sides already. Look at my corrected code below. I used all the same tests you did, but I took out the all the mains you had, made each Triangle have a unique name, and for Triangles that had the same sides, I didn't make a new variable for them since there was a Triangle already with that sides and so used that variable for the test.
class Triangle {

private int sd1, sd2, sd3;

public Triangle (int side1, int side2, int side3) {
sd1 = side1;
sd2 = side2;
sd3 = side3;
}

public boolean isEquilateral() {
if ((sd1 == sd2) && (sd2 == sd3))
return true;
else
return false;
}

public boolean isRight() {
if ((sd1 * sd1) + (sd2 * sd2) == (sd3 * sd3))
return true;
else
return false;
}

public boolean isScalene() {
if ((sd1 != sd2) && (sd1 != sd3) && (sd2 != sd3))
return true;
else
return false;
}

public boolean isIsosceles() {
if ((sd1 == sd2) && (sd1 != sd3))
return true;
else if ((sd2 == sd3) && (sd1 != sd2))
return true;
else if ((sd1 == sd3) && (sd1 != sd2))
return true;
else
return false;
}

public static void main (String[] args) {

Triangle myTriangle1 = new Triangle(5,5,5);

if (myTriangle1.isEquilateral())
System.out.println ("This is an equilateral triangle.");
else
System.out.println ("This is NOT an equilateral triangle.");

Triangle myTriangle2 = new Triangle(5,3,6);

if (myTriangle2.isEquilateral())
System.out.println ("This is an equilateral triangle.");
else
System.out.println ("This is not an equilateral triangle.");

Triangle myTriangle3 = new Triangle(3,4,5);

if (myTriangle3.isRight())
System.out.println ("This is a right triangle.");
else
System.out.println ("This is NOT a right triangle.");

Triangle myTriangle4 = new Triangle(3,5,6);

if (myTriangle4.isRight())
System.out.println ("This is a right triangle.");
else
System.out.println ("This is NOT a right triangle.");

if (myTriangle3.isIsosceles())
System.out.println ("This is an Isosceles triangle.");
else
System.out.println ("This is NOT an Isosceles triangle.");

Triangle myTriangle5 = new Triangle(3,3,5);

if (myTriangle5.isIsosceles())
System.out.println ("This is an Isosceles triangle.");
else
System.out.println ("This is NOT an Isosceles triangle.");

if (myTriangle3.isScalene())
System.out.println ("This is an scalene triangle.");
else
System.out.println ("This is NOT an scalene triangle.");

if (myTriangle5.isScalene())
System.out.println ("This is an Scalene triangle.");
else
System.out.println ("This is NOT an Scalene triangle.");
}
}
Also, make sure you enclose your code in CODE tags. Just select and highlight your code and then select the pound button up above where you see all those symbols.

-Shane

katCT
11-10-2005, 02:05 AM
I have a question about the phone number problem at the top. I guess it is suppose to ask for the area code and everything else and then print out the whole phone number. Mine only prints out (nnn)-nnn-nnnn
do you know how I can fix this?

TheShaner
11-10-2005, 03:49 PM
Wait, do you want it to ask for the entire phone number or do you want it to ask for it in separate parts? Then what is the output suppose to be? You can change the (nnn)-nnn-nnnn in the code above to anything like (954)-213-2352 and it'll work fine. Do you just want that part to be able to be entered by a user input instead of hard coded into the program? That is what the program below does.
import java.io.*;
import java.util.StringTokenizer;

public class phone_Tokens {

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

String areacode, exchange, extension, input;

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Please enter a phone number in the format (nnn)-nnn-nnnn: ");
input = stdin.readLine();
StringTokenizer phone = new StringTokenizer (input, "-");

areacode = phone.nextToken();
exchange = phone.nextToken();
extension = phone.nextToken();

System.out.println ("Areacode: " + areacode);
System.out.println ("Exchange: " + exchange);
System.out.println ("Extension: " + extension);
}
}
-Shane

katCT
11-10-2005, 04:20 PM
thank you so much that was exactly what i meant.:)