need some help with my java assignment

Write a program that creates an instance of a Triangle class you creat

Page 1 of 1

10 Replies - 2533 Views - Last Post: 17 March 2010 - 08:16 AM Rate Topic: -----

#1 ryclegman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-February 10

need some help with my java assignment

Posted 10 March 2010 - 08:00 AM

Write a program that creates an instance of a Triangle class you created. The Triangle class will have data fields for the 3 sides, default constructor and constructor with parameters, method for getting the area of the triangle.
The program will all the user to enter the dimensions for the sides of the triangle.

-- I did this and got an email back from my instructor with

Quote

"two things
You need to create a separate java file called triangle and then put your logic in it and create an instance of it in your main java file.

Secondly, your math is not good. If you enter 1 1 1 you get zero as the area
that is due to integer division.."



import javax.swing.JOptionPane;


public class RAC08_01 {
	
	public static void main(String[] args)
		  
		  {
			
		   
		   
		        
		        RAC08_01 triangle = new RAC08_01();
		  
		     
		        String side1;
		     
		      String side2 ;
		      
		      String base;
		      String height;
		        
		      
		      
		      side1=JOptionPane.showInputDialog("Enter length of side 1");
		       side2=JOptionPane.showInputDialog("Enter length of side 2");
		       
		        base=JOptionPane.showInputDialog("Enter length of the base");
		        height=JOptionPane.showInputDialog("Enter height of the triangle");
		       
		        int number1;
		        
		        int number2;
		        
		        int number3;
		        int number4;
		        double area;
		        
		        
		        number1 = Integer.parseInt( side1 );
		        
		        number2 = Integer.parseInt( side2 );
		      
		        number3 = Integer.parseInt( base );
		        number4 = Integer.parseInt( height );
		        
		      area=((number3*number4)/2);
		        
		        
		  

		 
System.out.println("The length of side 1 of your triangle is " +  number1 + ", the length of side 2 is " + number2 + ", the length of the base is " + number3 +", the height of your triangle is " +  number4);
		   

		   
System.out.println("The area of your triangle is " +  area);
		} }




I do not know how to create a java program with more then 1 .java page.... thanks for the help

This post has been edited by ryclegman: 10 March 2010 - 08:01 AM


Is This A Good Question/Topic? 0
  • +

Replies To: need some help with my java assignment

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10781
  • View blog
  • Posts: 40,155
  • Joined: 27-December 08

Re: need some help with my java assignment

Posted 10 March 2010 - 08:08 AM

To use two separate Java files, keep them in the same directory. So if you have public class Triangle and public class Lab, your .java files will be name Triangle.java and Lab.java. Then, so long as they're in the same directory (or you import the Triangle class into your Lab class if they're in different directories), you can simply work with the Triangle class from your Lab class. So something like:

public class Triangle{

   public Triangle(){
      //your logic goes here
    }
}

public class Lab{
  public static void main(String[] args){
      Triangle t = new Triangle();
  }
}



As for Java integer division, basically, if you divide two integers, the result is the number of times the denominator goes evenly into the numerator. So for example:
int x = 33/4; //x = 8
int y = 1/2; //y = 0



You can get around this by using double type variables, and making either the numerator or denominator a double.
Was This Post Helpful? 0
  • +
  • -

#3 ryclegman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-February 10

Re: need some help with my java assignment

Posted 10 March 2010 - 08:15 AM

View Postmacosxnerd101, on 10 March 2010 - 07:08 AM, said:

To use two separate Java files, keep them in the same directory. So if you have public class Triangle and public class Lab, your .java files will be name Triangle.java and Lab.java. Then, so long as they're in the same directory (or you import the Triangle class into your Lab class if they're in different directories), you can simply work with the Triangle class from your Lab class. So something like:

public class Triangle{

   public Triangle(){
      //your logic goes here
    }
}

public class Lab{
  public static void main(String[] args){
      Triangle t = new Triangle();
  }
}



As for Java integer division, basically, if you divide two integers, the result is the number of times the denominator goes evenly into the numerator. So for example:
int x = 33/4; //x = 8
int y = 1/2; //y = 0



You can get around this by using double type variables, and making either the numerator or denominator a double.


okay and im lost on how to creat a .java file in eclipse... our instructor is worthless really.. he doesn't answer questions and is no help unfortunately
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10781
  • View blog
  • Posts: 40,155
  • Joined: 27-December 08

Re: need some help with my java assignment

Posted 10 March 2010 - 08:18 AM

In your project, you should be able to create a new Java Class. Make sure you save both your classes to the same package. These will be your .java files.
Was This Post Helpful? 0
  • +
  • -

#5 ryclegman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-February 10

Re: need some help with my java assignment

Posted 10 March 2010 - 08:20 AM

View Postmacosxnerd101, on 10 March 2010 - 07:18 AM, said:

In your project, you should be able to create a new Java Class. Make sure you save both your classes to the same package. These will be your .java files.



okay great thanks for your help !!!!
Was This Post Helpful? 0
  • +
  • -

#6 ryclegman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-February 10

Re: need some help with my java assignment

Posted 11 March 2010 - 08:29 AM

View Postryclegman, on 10 March 2010 - 07:20 AM, said:

View Postmacosxnerd101, on 10 March 2010 - 07:18 AM, said:

In your project, you should be able to create a new Java Class. Make sure you save both your classes to the same package. These will be your .java files.



okay great thanks for your help !!!!


this is so frustrating ... idk where to go from here and my instructor sucks ....


import java.util.Scanner;
import javax.swing.JOptionPane;
 

	public class RAC08_01{
	  public static void main(String[] args){
		 
		  
		   triangle triangle = new triangle();
		  
		  
		  }
	}
		 

		  


import javax.swing.JOptionPane;


public class triangle {

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

		 {
				
			   
			 //triangle RAC08_01 = new triangle();
		        
		       
		  
		     
		        String side1;
		     
		      String side2 ;
		      
		      String base;
		      String height;
		        
		      
		      
		      side1=JOptionPane.showInputDialog("Enter length of side 1");
		       side2=JOptionPane.showInputDialog("Enter length of side 2");
		       
		        base=JOptionPane.showInputDialog("Enter length of the base");
		        height=JOptionPane.showInputDialog("Enter height of the triangle");
		       
		        int number1;
		        
		        int number2;
		        
		        int number3;
		        int number4;
		        double area;
		        
		        
		        number1 = Integer.parseInt( side1 );
		        
		        number2 = Integer.parseInt( side2 );
		      
		        number3 = Integer.parseInt( base );
		        number4 = Integer.parseInt( height );
		        
		      area=((number3*number4)/2);
		        
		        
		  

		 
System.out.println("The length of side 1 of your triangle is " +  number1 + ", the length of side 2 is " + number2 + ", the length of the base is " + number3 +", the height of your triangle is " +  number4);
		   

		   
System.out.println("The area of your triangle is " +  area);
	
	
	}

}}


Was This Post Helpful? 0
  • +
  • -

#7 PFJ  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 05-March 10

Re: need some help with my java assignment

Posted 11 March 2010 - 02:30 PM

howdy:
I know how some teachers can be (got one right now). Anyway, I'm not sure if I understand exactly what you're trying to do since I'm new to Java myself, but hopefully this helps. I didn't use JOptionPane, but I think this will probably give you the gist of how to use separate files and manipulate information going between them:
//file:  Triangle.java
public class Triangle{
    double area;

    public double Triangle(double base, double height){
        area = ((base*height)/ 2);
        return area;
    }//close method Triangle()
}//close class Triangle


and here's the main file
//file:  Lab.java
import java.util.Scanner;

public class Lab{
  public static void main(String[] args){
      Triangle t = new Triangle();
      Scanner in = new Scanner(System.in);

      //all the variables
      int side1;
      int side2;
      int base;
      int height;
      
      double area;

      //get the user's input
      System.out.println("Enter length of side 1: ");
      side1 = in.nextInt();
      System.out.println("Enter the length of side 2: ");
      side2 = in.nextInt();
      System.out.println("Enter the length of the base: ");
      base = in.nextInt();
      System.out.println("Enter the height of the triangle: ");
      height = in.nextInt();

      //print statements
      System.out.println("The length of side 1 is " + side1 + ".");
      System.out.println("The length of side 2 is " + side2 + ".");
      System.out.println("The length of the base is " + base + ".");
      System.out.println("The height of the triangle is " + height + ".");
      System.out.println("The area of the triangle is " + t.Triangle(base, height) + ".");
  }//close main
}//close class Lab


For me at least, once I seen this type of stuff done once, it was easy to remember. I hope this helps! :)

This post has been edited by PFJ: 11 March 2010 - 02:34 PM

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10781
  • View blog
  • Posts: 40,155
  • Joined: 27-December 08

Re: need some help with my java assignment

Posted 11 March 2010 - 03:32 PM

@ryclegman: The main() method tells the JVM where the logic flow of the program begins. While syntactically you can have main() methods in each class, this should only be done for testing purposes. By this, I mean you shouldn't be defining everything your triangle class does in its main() method. Instead, your class should be a blueprint for a Triangle object. Think about what attributes a triangle has (these will be your instance and global variables), and what methods or functionality you want from it (these will be your methods).

Also, we understand your instructor isn't great- not many Intro to Programming teachers are. However, simply telling us "My teacher sucks; I'm a lost newbie" isn't helping us figure out what you don't get. So rather than complaining about him, tell us what you don't get and show us your efforts so we can better help you. Note that if you are truly lost, talk us through your thought process, what you do know, and how you think you should proceed. We will then be happy to better explain these concepts to you, and get you back on the right path. :)
Was This Post Helpful? 0
  • +
  • -

#9 ryclegman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-February 10

Re: need some help with my java assignment

Posted 15 March 2010 - 08:36 AM

View Postmacosxnerd101, on 11 March 2010 - 02:32 PM, said:

@ryclegman: The main() method tells the JVM where the logic flow of the program begins. While syntactically you can have main() methods in each class, this should only be done for testing purposes. By this, I mean you shouldn't be defining everything your triangle class does in its main() method. Instead, your class should be a blueprint for a Triangle object. Think about what attributes a triangle has (these will be your instance and global variables), and what methods or functionality you want from it (these will be your methods).

Also, we understand your instructor isn't great- not many Intro to Programming teachers are. However, simply telling us "My teacher sucks; I'm a lost newbie" isn't helping us figure out what you don't get. So rather than complaining about him, tell us what you don't get and show us your efforts so we can better help you. Note that if you are truly lost, talk us through your thought process, what you do know, and how you think you should proceed. We will then be happy to better explain these concepts to you, and get you back on the right path. :)

Thanks a ton guys expectantly PFJ! i understand it 100 % now.. i havent seen how or what it should look like set up! i will understand how to put my next assignments togeather ... And sorry macosxnerd101.. ha i just got really frustrated i understand what i shall do next time.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10781
  • View blog
  • Posts: 40,155
  • Joined: 27-December 08

Re: need some help with my java assignment

Posted 15 March 2010 - 08:46 AM

Not a problem. I understand that you are frustrated with your teacher; however, complaining about him won't help us help you. :)
Was This Post Helpful? 0
  • +
  • -

#11 ryclegman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-February 10

Re: need some help with my java assignment

Posted 17 March 2010 - 08:16 AM

View Postmacosxnerd101, on 15 March 2010 - 07:46 AM, said:

Not a problem. I understand that you are frustrated with your teacher; however, complaining about him won't help us help you. :)

Ha True that!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1