Click here to Skip to main content
15,895,423 members
Home / Discussions / Java
   

Java

 
GeneralRe: help: equals() method to compare the attributes Pin
Nagy Vilmos7-Oct-11 7:12
professionalNagy Vilmos7-Oct-11 7:12 
GeneralRe: help: equals() method to compare the attributes Pin
mastdesi7-Oct-11 8:52
mastdesi7-Oct-11 8:52 
AnswerRe: help: equals() method to compare the attributes Pin
Nagy Vilmos6-Oct-11 22:43
professionalNagy Vilmos6-Oct-11 22:43 
GeneralRe: help: equals() method to compare the attributes Pin
mastdesi7-Oct-11 5:17
mastdesi7-Oct-11 5:17 
GeneralRe: help: equals() method to compare the attributes Pin
Nagy Vilmos7-Oct-11 7:14
professionalNagy Vilmos7-Oct-11 7:14 
QuestionPlease help with java assignment Pin
mastdesi6-Oct-11 4:43
mastdesi6-Oct-11 4:43 
AnswerRe: Please help with java assignment Pin
Nagy Vilmos6-Oct-11 5:46
professionalNagy Vilmos6-Oct-11 5:46 
GeneralRe: Please help with java assignment Pin
mastdesi6-Oct-11 6:19
mastdesi6-Oct-11 6:19 
the file is a pdf. sorry about the posting everywhere. its due tomorrow and i am off today so i can work on it. but i can't start off. the teacher says its like the last assignment with arrays used. Now i don't understand is how can i use this array concept with this inheritance. since every extended class has different parameters.

I did this last assignment with your help and others here.
last assignment:
Java
public class Polynomial
{
	private int[] coeff = new int[5];
	public Polynomial()
	{
		coeff[0] = 0;
		coeff[1] = 0;
		coeff[2] = 0;
		coeff[3] = 0;
		coeff[4] = 0;
	}
	// Constructor with 5 parameter 
	public Polynomial(int c0, int c1, int c2, int c3, int c4)
	{
		this.coeff[0] = c4;
		this.coeff[1] = c3;
		this.coeff[2] = c2;
		this.coeff[3] = c1;
		this.coeff[4] = c0;
	}
	// Copy constructor  
	public Polynomial(Polynomial p)
	{
	    coeff = (int[])p.coeff.clone();
	 
	}
	
	// Get Method
	public int getCoeff(int i)
	{
		return this.coeff[i];
	}
	
	// Set Method
	public boolean setCoeff(int pos, int num)
	{
		if (pos <= 4 || pos >= 0 )
			{
			coeff[pos] = num;
			return true;
			}
		else
			return false;
	}

	// toString
	public String toString() 
	{
		String s = "";
		for (int c = 4; c >=0; c--)
		{
			if (coeff[c] !=0)
				if (c != 0)
				{
					if (coeff[c] < 0 && c!=4)
						s = s + coeff[c] + "X^" + c + " ";
					if (coeff[c] > 0 && c!=4)
						s = s + "+ " + coeff[c] + "X^" + c + " ";
					if (c == 4)
						s = s + coeff[c] + "X^" + c + " ";
				}
				else
				{
					if (coeff[c] > 0)
						s = s + "+ " + coeff[c];
					else
						s = s + coeff[c];
				}
		}
		return s;
	}
	
	// Equals
	public boolean equals(Polynomial p)
	{
		if ((p.coeff[4] != coeff[4]) ||(p.coeff[3] != coeff[3]) ||(p.coeff[2] != coeff[2]) ||(p.coeff[1] != coeff[1]) ||(p.coeff[0] != coeff[0]) )
			return false;
		return true;
	}
	
	// Add
	public Polynomial add(Polynomial p)
	{
		return (new Polynomial(p.coeff[4]+coeff[4],p.coeff[3]+coeff[3],p.coeff[2]+coeff[2],p.coeff[1]+coeff[1],p.coeff[0]+coeff[0]));
	}

	// Derivative
	public Polynomial derive()
	{
		return (new Polynomial(0,coeff[4]*4,coeff[3]*3,coeff[2]*2,coeff[1]));
	}

	// Evaluate
	public double evaluate(double x)
	{
	    return (coeff[4] * x*x*x*x) +
	        (coeff[3] * x*x*x) +
	        (coeff[2] * x*x) +
	        (coeff[1] * x) +
	        coeff[0];
	}
	
	// NumberOfTerms
	public int numberOfTerms()
	{
		int n = 0;
		if (coeff[4] != 0)
			n +=1;
		if (coeff[3] != 0)
			n +=1;
		if (coeff[2] != 0)
			n +=1;
		if (coeff[1] != 0)
			n +=1;
		if (coeff[0] != 0)
			n +=1;
		return n;
	}

}


The driver of pervious assignment
Java
import java.util.*;

public class Driver 
{
	public static void main(String[] args) 
	{
		System.out.println("*~*~*~*Program writen by *~*~*~*");
		Scanner input = new Scanner(System.in);

		Polynomial[] poly = new Polynomial[8];
		int[] tmp = new int[5];
		
		// Values for Polynomial 0
		System.out.println("Enter a polynomial");
		for(int i=0; i<5;i++) 
		{
			System.out.print("Coefficient for X^" + i + ": ");
			int coeff = input.nextInt();
			tmp[i] = coeff;
		}
		
		
		poly[0] = new Polynomial(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
		
		// Values for Polynomial 1
		System.out.println("Enter a polynomial");
		for(int i=0; i<5;i++) 
		{
			System.out.print("Coefficient for X^" + i + ": ");
			int coeff = input.nextInt();
			tmp[i] = coeff;
		}
		
		poly[1] = new Polynomial(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
		
		Random obj= new Random();
		// Random values from 0-100 for Polynomial 2
		for(int i=0; i<5;i++) 
		{
			int coeff = obj.nextInt(100);
			tmp[i] = coeff;
		}
		
		poly[2] = new Polynomial(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
		
		
		// Random values from 0-100 for Polynomial 3
		for(int i=0; i<5;i++) 
		{
			int coeff = obj.nextInt(100);
			tmp[i] = coeff;
		}
		
		poly[3] = new Polynomial(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
		
		
		// Random values from 0-100 for Polynomial 4
		for(int i=0; i<5;i++) 
		{
			int coeff = obj.nextInt(100);
			tmp[i] = coeff;
		}
		
		poly[4] = new Polynomial(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
		
		// Polynomial 5
		poly[5] = new Polynomial(poly[0].add(poly[2]));
		
		// Polynomial 6
		poly[6] = new Polynomial(poly[1]);
		for (int i = 0; i <= 4; i++)
		{
			int m = poly[6].getCoeff(i);
			m *=3;
			boolean b = poly[6].setCoeff(i,m);
		}
		
		// Polynomial &
		poly[7] = new Polynomial(poly[4].derive());	
		
		// Ask the user to input x
		System.out.print("Enter a double value for X: ");
		double x = input.nextDouble();
		
		// Print out the results for the eight polynomials
		for (int p = 0; p < 8; p++) 
		{
		    System.out.println("Polynomial " + p + " : " + poly[p] +
		            " has " + poly[p].numberOfTerms() +
		            " terms and evaluates to " + poly[p].evaluate(x) +
		            " for x = " + x);
		}
		
		// Comparisons
		for (int p = 1; p < 8; p++)
		{
			if ( poly[0].equals(poly[p]) )
				System.out.println("Polynomial 0 and Polynomial " + p + " are equal.");
			else
				System.out.println("Polynomial 0 and Polynomial " + p + " are not equal.");
		}
		
		poly[5] = new Polynomial(poly[1]);
		if ( poly[5].equals(poly[1]) )
			System.out.println("Polynomial 5 and Polynomial 1 are now equal after the change.");

	}

}

GeneralRe: Please help with java assignment Pin
TorstenH.6-Oct-11 19:44
TorstenH.6-Oct-11 19:44 
GeneralRe: Please help with java assignment Pin
Nagy Vilmos6-Oct-11 23:31
professionalNagy Vilmos6-Oct-11 23:31 
GeneralRe: Please help with java assignment Pin
mastdesi6-Oct-11 6:33
mastdesi6-Oct-11 6:33 
GeneralRe: Please help with java assignment Pin
Nagy Vilmos6-Oct-11 23:58
professionalNagy Vilmos6-Oct-11 23:58 
AnswerRe: Please help with java assignment Pin
I.explore.code12-Oct-11 4:31
I.explore.code12-Oct-11 4:31 
QuestionMultilevelqueue Pin
jhencer1111085-Oct-11 12:40
jhencer1111085-Oct-11 12:40 
AnswerRe: Multilevelqueue Pin
Richard MacCutchan5-Oct-11 21:49
mveRichard MacCutchan5-Oct-11 21:49 
AnswerRe: Multilevelqueue Pin
Nagy Vilmos5-Oct-11 22:35
professionalNagy Vilmos5-Oct-11 22:35 
QuestionTroubble with "new" Pin
Tor Danielsen4-Oct-11 11:00
Tor Danielsen4-Oct-11 11:00 
AnswerRe: Troubble with "new" Pin
DaveAuld4-Oct-11 12:03
professionalDaveAuld4-Oct-11 12:03 
GeneralRe: Troubble with "new" Pin
Tor Danielsen4-Oct-11 12:28
Tor Danielsen4-Oct-11 12:28 
GeneralRe: Troubble with "new" Pin
DaveAuld4-Oct-11 13:37
professionalDaveAuld4-Oct-11 13:37 
AnswerRe: Troubble with "new" Pin
Luc Pattyn4-Oct-11 13:40
sitebuilderLuc Pattyn4-Oct-11 13:40 
AnswerRe: Troubble with "new" Pin
Nagy Vilmos4-Oct-11 21:47
professionalNagy Vilmos4-Oct-11 21:47 
AnswerRe: Troubble with "new" Pin
Richard MacCutchan4-Oct-11 21:50
mveRichard MacCutchan4-Oct-11 21:50 
AnswerRe: Troubble with "new" Pin
TorstenH.4-Oct-11 23:57
TorstenH.4-Oct-11 23:57 
GeneralRe: Troubble with "new" Pin
Nagy Vilmos5-Oct-11 0:12
professionalNagy Vilmos5-Oct-11 0:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.