Results 1 to 12 of 12
 02272011, 03:17 AM #1Member
 Join Date
 Feb 2011
 Posts
 7
 Rep Power
 0
Need Help For doing if else statement
First of all, I never learn any java or C programming.. so I am new to any programming.
I got an assignment with skeleton provided from professor, but there are errors and I do not know how to fix it.
It is really easy to you guys.. right?
I want to finish this assignment so that I can focus on my other classes..
i will also post what I wrote in #2:D
Thanks for helping me...:o
He gave us simple instruction, input and desire output which are the following:
From Prof. :This assignment will give you practice with if statements, strings, polynomials of degree 2 and Math.sqrt.
The file available on the Web page is the main(). I have written some of it and you must complete it. The sections to be
completed are marked // Complete.
I will run your program using the input file posted on the Web page. Your output must match the desired output posted on the Web page. Be sure to test the following values:
a < 1, a == 1, a = 1, a > 1, b < 1, b == 1, b == 0, b == 1, b > 1
c < 1, c == 1, c == 0, c == 1, c > 1.
Input:
Java Code:8 2 2 2 1 1 1 1 2 1 1 1 1 1 0 0 1 1 1 1 2 2 2 2 1 1 0 4 0 1 0 25 1
Skeleton:
Java Code:/** * */ package cecs174; import java.io.File; import java.util.Scanner; import java.io.FileNotFoundException; /** * @author murgolo * */ public class AssignmentFourMain { static final String FNAME = "ass4_input.txt"; /** * @param args */ public static void main(String[] args) { Scanner inFile = null; try { inFile = new Scanner(new File(FNAME)); } catch(FileNotFoundException e) { System.out.println("Error: " + e.getMessage()); System.out.println("Make sure " + FNAME + " is in the project's top level directory"); System.exit(1); } int count = inFile.nextInt(); double checkSum = 0; for(int ii = 0; ii < count; ++ii) { int a = inFile.nextInt(); int b = inFile.nextInt(); int c = inFile.nextInt(); int x = inFile.nextInt(); String poly = ""; // COMPLETE System.out.println("Polynomial is " + poly); int discriminate ; // COMPLETE System.out.println("The discriminate is " + discriminate); int numRealRoots = 0, numImagRoots = 0; double root1 = 0.0, root2 = 0.0; // COMPLETE System.out.println("Number of real roots = " + numRealRoots); System.out.println("Number of imaginary roots = " + numImagRoots); System.out.print("Equation " + poly + " = 0 has "); // COMPLETE System.out.println("The value of " + poly + " at " + x + " is: "); // COMPLETE checkSum += root1 + root2; } System.out.println("Check sum is " + checkSum); inFile.close(); System.exit(0); } }
Desire output
(a,b,c,x) = (2,2,2,1)
Polynomial is 2x^2  2x  2
The discriminate is 12
Number of real roots = 0
Number of imaginary roots = 2
Equation 2x^2  2x  2 = 0 has no real roots
The value of 2x^2  2x  2 at 1 is: 6
(a,b,c,x) = (1,1,1,2)
Polynomial is x^2  x  1
The discriminate is 3
Number of real roots = 0
Number of imaginary roots = 2
Equation x^2  x  1 = 0 has no real roots
The value of x^2  x  1 at 2 is: 7
(a,b,c,x) = (1,1,1,1)
Polynomial is x^2  x  1
The discriminate is 5
Number of real roots = 2
Number of imaginary roots = 0
Equation x^2  x  1 = 0 has two real roots = (1.618033988749895,0.6180339887498949)
The value of x^2  x  1 at 1 is: 1
(a,b,c,x) = (1,0,0,1)
Polynomial is x^2
The discriminate is 0
Number of real roots = 1
Number of imaginary roots = 0
Equation x^2 = 0 has one real root = 0.0
The value of x^2 at 1 is: 1
(a,b,c,x) = (1,1,1,2)
Polynomial is x^2 + x + 1
The discriminate is 3
Number of real roots = 0
Number of imaginary roots = 2
Equation x^2 + x + 1 = 0 has no real roots
The value of x^2 + x + 1 at 2 is: 3
(a,b,c,x) = (2,2,2,1)
Polynomial is 2x^2 + 2x + 2
The discriminate is 12
Number of real roots = 0
Number of imaginary roots = 2
Equation 2x^2 + 2x + 2 = 0 has no real roots
The value of 2x^2 + 2x + 2 at 1 is: 6
(a,b,c,x) = (1,0,4,0)
Polynomial is x^2  4
The discriminate is 16
Number of real roots = 2
Number of imaginary roots = 0
Equation x^2  4 = 0 has two real roots = (2.0,2.0)
The value of x^2  4 at 0 is: 4
(a,b,c,x) = (1,0,25,1)
Polynomial is x^2  25
The discriminate is 100
Number of real roots = 2
Number of imaginary roots = 0
Equation x^2  25 = 0 has two real roots = (5.0,5.0)
The value of x^2  25 at 1 is: 24
Check sum is 1.0Last edited by Fubarable; 02272011 at 03:23 AM.

Moderator edits:
1) moved from Eclipse forum to the New To Java forum for closer forumtopic alignment.
2) code tags added to help make code and data readable.
 02272011, 03:38 AM #3Member
 Join Date
 Feb 2011
 Posts
 7
 Rep Power
 0
This is how far I go..
I don't know how to write .... I am really stuck at this point.:(
The following is the things I wrote.. and a printscreen (to let u see the errors):)
/**
*
*/
package cecs174;
/**
* @author Juven
*
*/
public class AssignmentFour
{
static final String FNAME = "ass4_input.txt";
/**
* @param args
*/
public static void main(String[] args)
{
Scanner inFile = null;
try
{
inFile = new Scanner(new File(FNAME));
}
catch(FileNotFoundException e)
{
System.out.println("Error: " + e.getMessage());
System.out.println("Make sure " + FNAME + " is in the project's top level directory");
System.exit(1);
}
int count = inFile.nextInt();
double checkSum = 0;
for(int ii = 0; ii < count; ++ii)
{
int a = inFile.nextInt();
int b = inFile.nextInt();
int c = inFile.nextInt();
int x = inFile.nextInt();
String poly = "";
// COMPLETE
poly = poly + a + " x^2 " + " + " + b + " x " + " + " + c ;
System.out.println("Polynomial is " + poly);
int discriminate ;
// COMPLETE
discriminate = b * b  4 * a * c;
if ( discriminate < 0 );
numRealRoots = 0;
numImagRoots = 2;
else if ( discriminate == 0 )
numRealRoots = 1;
numImagRoots = 0;
else ( discriminate > 0 )
numRealRoots = 2;
numImagRoots = 0;
System.out.println("The discriminate is " + discriminate);
int numRealRoots = 0, numImagRoots = 0;
double root1 = 0.0, root2 = 0.0;
// COMPLETE
System.out.println("Number of real roots = " + numRealRoots);
System.out.println("Number of imaginary roots = " + numImagRoots);
System.out.print("Equation " + poly + " = 0 has ");
// COMPLETE
double polyvalue;
polyvalue = a * x * x + b * x + c ;
System.out.println("The value of " + poly + " at " + x + " is: "); // COMPLETE
checkSum += root1 + root2;
}
System.out.println("Check sum is " + check Sum);
inFile.close();
System.exit(0);
}
}
 02272011, 03:43 AM #4Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,643
 Rep Power
 13
The usual way of doing things is to post the compiler messages if you can't understand them.
In this case one source of error stands out straight away: use braces with every if and else block, even ones that are only one line long.
Java Code:if(something) [b][color=red]{[/color][/b] // code [b][color=red]}[/color][/b] else if(somethingElse) [b][color=red]{[/color][/b] // code [b][color=red]}[/color][/b] else [b][color=red]{[/color][/b] // code [b][color=red]}[/color][/b]
 02272011, 04:37 AM #5Member
 Join Date
 Feb 2011
 Posts
 7
 Rep Power
 0
oh.. braces for every if .. else if..
i want to ask..
where should i put my ass4_input.txt ??
there are three errors around the scanner file.. how to I fix that.
Thanks pbrockway
 02272011, 04:41 AM #6Member
 Join Date
 Feb 2011
 Posts
 7
 Rep Power
 0
comparing the desire out put and my code..
am I right to put numRealRoots and numImagRoots in that place?
 02272011, 04:59 AM #7Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,643
 Rep Power
 13
You're welcome.
Please post the compiler messages. I *think* you have not imported the Scanner class or the FileNotFoundException class, so the compiler will complain about that.
One thing: when you post code use the "code" tags. Put [code] at the start of the code and [/code] at the end. That will preserve the formatting. Most people (including me!) are more inclined to read the code if it is properly formatted.
 02272011, 05:14 AM #8Member
 Join Date
 Feb 2011
 Posts
 7
 Rep Power
 0
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Scanner cannot be resolved to a type
Scanner cannot be resolved to a type
File cannot be resolved to a type
FileNotFoundException cannot be resolved to a type
numRealRoots cannot be resolved to a variable
numImagRoots cannot be resolved to a variable
numRealRoots cannot be resolved to a variable
numImagRoots cannot be resolved to a variable
Syntax error on token ")", } expected
numRealRoots cannot be resolved to a variable
numImagRoots cannot be resolved to a variable
Syntax error on token "}", delete this token
Syntax error, insert "}" to complete ClassBody
at cecs174.AssignmentFour.main(AssignmentFour.java:19 )
I don't get what u mean by usingJava Code:put my codes here?
 02272011, 05:22 AM #9Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,643
 Rep Power
 13
Did you understand what I meant by having to include Scanner and FileNotFoundException?
Find out what package they belong to and put an import line for each just below the package statement at the top. (Check notes/textbook etc for examples of this.)
numRealRoots cannot be resolved to a variable
This means that you have not declared numRealRoots anywhere (you have to tell the compiler that it's an int). You need a line like
Java Code:int numRealRoots;
before you use that variable. You did that for discriminant and you have to do it for all variables.
 02272011, 05:30 AM #10Member
 Join Date
 Feb 2011
 Posts
 7
 Rep Power
 0
after u see the desire output.
how should I declare the numRealRoots and numImagRoots?
do u mind give me a hand, or .. help me to write it?
i don;t have the java book with me..
oh boy.. java really makes me headache.. haha
r u cs major?
 02272011, 05:57 AM #11Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,643
 Rep Power
 13
I don't write code for people.
As far as including the necessary imports and declaring variables before use is concerned (and that's a fair number of the compiler messages) your best bet is to consult some text or online tutorial. Oracle's Tutorial covers the basics in the section Learning the Java Language.
Many other online tutorials exist  including one linked to here within the lat couple of days that looked very good.
 02272011, 06:22 AM #12Member
 Join Date
 Feb 2011
 Posts
 7
 Rep Power
 0
Similar Threads

If Statement in SQL
By Steffi1013 in forum JDBCReplies: 6Last Post: 04102010, 03:19 PM 
need help in sql statement
By chyeeqi in forum JDBCReplies: 6Last Post: 03282010, 07:49 PM 
for statement help
By helpisontheway in forum New To JavaReplies: 5Last Post: 11142009, 05:14 PM 
The if Statement, need some help!
By Keno777 in forum New To JavaReplies: 6Last Post: 10242009, 12:53 AM 
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08012007, 04:45 PM
Bookmarks