Click here to Skip to main content
16,005,389 members
Home / Discussions / Java
   

Java

 
AnswerRe: how using sprite in j2me Pin
Peter_in_278028-Sep-11 13:23
professionalPeter_in_278028-Sep-11 13:23 
GeneralRe: how using sprite in j2me Pin
h1428-Sep-11 14:42
h1428-Sep-11 14:42 
GeneralRe: how using sprite in j2me Pin
Firo Atrum Ventus28-Sep-11 15:26
Firo Atrum Ventus28-Sep-11 15:26 
Questionspeech recognition Pin
gif202027-Sep-11 9:29
gif202027-Sep-11 9:29 
AnswerRe: speech recognition Pin
Richard MacCutchan27-Sep-11 10:02
mveRichard MacCutchan27-Sep-11 10:02 
GeneralRe: speech recognition Pin
gif202028-Sep-11 12:11
gif202028-Sep-11 12:11 
GeneralRe: speech recognition Pin
Richard MacCutchan28-Sep-11 12:22
mveRichard MacCutchan28-Sep-11 12:22 
QuestionPolynomial class - need some assistance with it Pin
mastdesi22-Sep-11 16:17
mastdesi22-Sep-11 16:17 
I can't figure out a few things.
- When i do the derivative it still keeps the exponents like x^4 when it should be x^3, when it should be doing -1 for the exponents.
- the toString method is suppose to return only the non zero terms.
- in poly 6 i want to store coefficients/values of 1 multiplied by 3
- and compare each polynomial to each other and report whether they are the same. like compare 0 with 1-7 and then 1 with 2-7 and so on.

I can seem to figure out a way for it without getting a error when compiling. Been trying for hours today and in the end thought of asking here for some assistance.

assignment pdf file can be downloaded here http://www.2shared.com/document/7YITxJL1/A1_online.html

Almost completed Driver Class
Java
import java.util.*;

public class Driver 
{
	public static void main(String[] args) 
	{
		
		Scanner input = new Scanner(System.in);

		Polynomial[] poly = new Polynomial[8];
		int[] tmp = new int[5];
		// Number for 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]);
		
		// Number for 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 numbers 0-100 for 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 numbers 0-100 for 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 numbers 0-100 for 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]);
		
		poly[5] = new Polynomial(poly[0].add(poly[2]));
		
		poly[7] = new Polynomial(poly[4].derive());	// the exponent should be -1 	
		
		System.out.print("Enter a double value for X: ");
		double x = input.nextDouble();
		
		poly[6] = new Polynomial(poly[1]); // has to be multiplied by 3
		
		System.out.println("Polynomial 0 : " + poly[0] + " has " + poly[0].numberOfTerms() + " terms and evaluates to " + poly[0].evaluate(x) + " for x = " + x);
		System.out.println("Polynomial 1 : " + poly[1] + " has " + poly[1].numberOfTerms() + " terms and evaluates to " + poly[1].evaluate(x) + " for x = " + x);
		System.out.println("Polynomial 2 : " + poly[2] + " has " + poly[2].numberOfTerms() + " terms and evaluates to " + poly[2].evaluate(x) + " for x = " + x);
		System.out.println("Polynomial 3 : " + poly[3] + " has " + poly[3].numberOfTerms() + " terms and evaluates to " + poly[3].evaluate(x) + " for x = " + x);
		System.out.println("Polynomial 4 : " + poly[4] + " has " + poly[4].numberOfTerms() + " terms and evaluates to " + poly[4].evaluate(x) + " for x = " + x);
		System.out.println("Polynomial 5 : " + poly[5] + " has " + poly[5].numberOfTerms() + " terms and evaluates to " + poly[5].evaluate(x) + " for x = " + x);
		System.out.println("Polynomial 6 : " + poly[6] + " has " + poly[6].numberOfTerms() + " terms and evaluates to " + poly[6].evaluate(x) + " for x = " + x);
		System.out.println("Polynomial 7 : " + poly[7] + " has " + poly[7].numberOfTerms() + " terms and evaluates to " + poly[7].evaluate(x) + " for x = " + x);
		
		

	}

}


Almost Completed Polynomial Class
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 setCoef(int pos, int num)
	{
		if (pos <= 4 || pos >= 0 )
			{
			coeff[pos] = num;
			return true;
			}
		else
			return false;
	}

	// toString
	public String toString() 
	{
		return coeff[4] + "X^4 + " +
	        coeff[3] + "X^3 + " +
	        coeff[2] + "X^2 + " +
	        coeff[1] + "X + " + coeff[0];
	}
	
	// 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(coeff[4]*4,coeff[3]*3,coeff[2]*2,coeff[1],0));
	}

	// 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];
	}
	
	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;
	}

}

AnswerRe: Polynomial class - need some assistance with it Pin
Nagy Vilmos22-Sep-11 23:59
professionalNagy Vilmos22-Sep-11 23:59 
GeneralRe: Polynomial class - need some assistance with it Pin
mastdesi23-Sep-11 5:50
mastdesi23-Sep-11 5:50 
QuestionHow to calculating Business Hours in java Pin
Member 423170021-Sep-11 15:54
Member 423170021-Sep-11 15:54 
AnswerRe: How to calculating Business Hours in java Pin
TorstenH.21-Sep-11 19:22
TorstenH.21-Sep-11 19:22 
AnswerRe: How to calculating Business Hours in java Pin
Richard MacCutchan21-Sep-11 21:47
mveRichard MacCutchan21-Sep-11 21:47 
AnswerRe: How to calculating Business Hours in java Pin
Nagy Vilmos21-Sep-11 21:49
professionalNagy Vilmos21-Sep-11 21:49 
GeneralRe: How to calculating Business Hours in java Pin
Richard MacCutchan22-Sep-11 0:10
mveRichard MacCutchan22-Sep-11 0:10 
GeneralRe: How to calculating Business Hours in java Pin
Nagy Vilmos22-Sep-11 0:37
professionalNagy Vilmos22-Sep-11 0:37 
AnswerRe: How to calculating Business Hours in java Pin
jschell22-Sep-11 8:41
jschell22-Sep-11 8:41 
GeneralRe: How to calculating Business Hours in java Pin
Nagy Vilmos22-Sep-11 22:57
professionalNagy Vilmos22-Sep-11 22:57 
GeneralRe: How to calculating Business Hours in java Pin
jschell23-Sep-11 12:19
jschell23-Sep-11 12:19 
GeneralRe: How to calculating Business Hours in java Pin
TorstenH.24-Sep-11 7:53
TorstenH.24-Sep-11 7:53 
GeneralJavascript in livecycle Pin
Incognito121-Sep-11 4:20
Incognito121-Sep-11 4:20 
GeneralRe: Javascript in livecycle Pin
Nagy Vilmos21-Sep-11 4:59
professionalNagy Vilmos21-Sep-11 4:59 
QuestionPlease check my program if it is correct Pin
mastdesi20-Sep-11 16:47
mastdesi20-Sep-11 16:47 
AnswerRe: Please check my program if it is correct Pin
Richard MacCutchan20-Sep-11 21:53
mveRichard MacCutchan20-Sep-11 21:53 
AnswerRe: Please check my program if it is correct Pin
Nagy Vilmos20-Sep-11 23:30
professionalNagy Vilmos20-Sep-11 23:30 

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.