1 Replies - 714 Views - Last Post: 29 November 2016 - 09:01 PM

#1 Heathersmithx   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 07-November 16

How to access my CLASS methods from MAIN method

Posted 29 November 2016 - 08:53 PM

I can't seem to be able to use my methods from my class, on the objects that I've created in my main method.
I'm trying to initilize my objects using a for loop. I'm calling my class methods on the object using
myFraction.Fraction(); But it gives me an error message.

public class Fraction
{
    private int numerator, denominator;
    private int divider = 2;
    private int wholeNumber = 0;
    private int newNumerator = 0;
    
    public Fraction()
    {
        numerator = 1;
        denominator = 1;
    }
    
    public Fraction(int n, int d)
    {
        numerator = n;
        denominator = d;
        
        }
    }




And here is my test in the main method. Both files are in the same package.

package testfractionclass;

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

public class TestFractionClass
{
    public static void main(String[] args)
    {
        Random rand = new Random();
        int numerator, denominator;
        
        int numberOption = 0;
        
        Fraction [] myFraction;
        myFraction = new Fraction[5];
        //Initilizes the Objects. Changes numerator and denominator to 1 in the class;
        for(int i = 0; i < myFraction.length; i++){
                                                    
              myFraction[i] = myFraction.Fraction();
              System.out.println(myFraction[i]); 
        }


run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: Array.Fraction
at testfractionclass.TestFractionClass.main(TestFractionClass.java:26)
C:\Users\JK\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

Is This A Good Question/Topic? 0
  • +

Replies To: How to access my CLASS methods from MAIN method

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: How to access my CLASS methods from MAIN method

Posted 29 November 2016 - 09:01 PM

Please do not open duplicate threads. See your original thread.

Also, the Student Campus is not the appropriate forum for programming help questions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1