Home Incompatible variable type issues
Reply: 0

Incompatible variable type issues

user3253
1#
user3253 Published in April 22, 2018, 6:05 pm

I'm a newcomer to coding, having just picked up my first coding unit this year on Java, and for one of our programming projects, we've been tasked to take a number sequence and derive the polynomial used to create that sequence in terms of "x."

The following segment of code (is it a segment? I mean, it's bloody long) comes from a Term class, whose job is to output a single term of the polynomial, and is called by the methods within 2 other classes - Polynomial and Sequence. It is an updated version of another method, except this one refrains from outputting stuff like x^1 in the place of x, 0x^3 in the place of 0, 2x^0 in the place of 2, etc. etc. etc.

Being a newbie to coding, my codes aren't exactly the prettiest thing you'll set your eyes on. Prepare yourselves for quite possibly the most disgusting piece of code you'll ever see:

// this function returns the term as a String for display: // see the sample file and the test program for the layout required

public String displayImproved()
{
    if(coefficient > 1)
    {
        if (exponent <= 1)
        {
            if (exponent = 1)
            {
                return " +" + coefficient + "x";
            }
            else
            {
                return " +" + coefficient;
            }
        if (exponent = 0)
        {
            return " +" + coefficient + "x^" + exponent;
        }

        }
        if(coefficient = 1)
        {
            if (exponent <= 1)
            {
                if (exponent = 1)
                {
                    return " +" + "x";
                }
                else
                {
                    return " +" + "1";
                }
            }
            else
            {
               return " +" + "x^" + exponent;
            }
        }
    }
    if (coefficient = 0)
    {
        return "";
    }
    if (coefficient < 0)
    {
        if (exponent <= 1)
        {
            if (exponent = 1)
            {
                return coefficient + "x";
            }
            else
            {
                return coefficient;
            }
        }
        else
        {
            return coefficient + "x^" + exponent;
        }
    }

}`

The sample file and test program I'm referring to can be found on the unit website: http://teaching.csse.uwa.edu.au/units/CITS1001/projects/project1/, which also has the details of the project we've been set if you want to try it out!

The issue I have here, dirtiness of the code aside, is the stream of error messages I get when I try to compile - for example, the very first error message I get comes on the line if (exponent = 1) and it tells me "incompatible types: int cannot be converted to boolean." For reference, the exponent variable is an integer, and the coefficient variable is a double. The program I'm using is BlueJ, if that helps. Not that I'm getting anywhere with my code soon!

So why is BlueJ telling me that I'm trying to switch between variable types, and why is it not working?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.312833 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO