|
Please, see point 2 here[^]
|
|
|
|
|
hi friends...
i am trying to do a simple java program in which i need two methods.....first method to get two values from the user.... 2nd method to find the sum of these two values......and the final main method to print the result....
i don't know why i am not able to get a result for that....can anybody help me to write the correct program,i need the correct code so that i can compare it with my code and find out my mistakes.....
|
|
|
|
|
why dont you give us your code to see, besides havent you heard of getters/setters try that...
we are what we are, but our true self comes with polishing of hardwork
|
|
|
|
|
below given is my code....
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Sum
{
public void getvalue() throws NumberFormatException, IOException
{
System.out.println("Enter two numbers :");
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
int a=Integer.parseInt(br.readLine());
int b=Integer.parseInt(br.readLine());
}
public int sum(int a,int b)
{
return(a+b);
}
public static void main(String args[]) throws NumberFormatException, IOException
{
Sum obj=new Sum();
obj.getvalue();
int sum=obj.sum(int a,int b);
System.out.println("Sum :"+ sum);
}
}
|
|
|
|
|
There are a few things which are going wrong.
1. int sum=obj.sum(int a,int b); is a big nono (you cannot call a function like this)
2. getvalue ask for two numbers but never actually stores it for use (unless you count the local int a and int b as storing it for further use.
You should store the value a and b on instance level, eg:
class Sum {
protected int a;
protected int b;
public void getvalue() throws NumberFormatException, IOException
{
System.out.println("Enter two numbers :");
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
}
public int sum(int a,int b)
{
return(a+b);
}
static void main(String args[]) {
Sum obj=new Sum();
obj.getvalue();
int sum=obj.sum(obj.a,obj.b);
System.out.println("Sum :"+ sum);
}
}
|
|
|
|
|
Take a look at some of the samples here[^] to understand how to handle variables and pass them into and out of methods.
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
hello i am developing a advance java application in which i have to put a web cam image capture feature for client simplicity, i have already used and patched method to browse,store and display image,but how to do it through java code(i mean the code invoke webcam device and capture image) ,please give suggestions ,and if please please provide examples with links and if available the third party api reference.
Thanks and Regards
Alok sharma
|
|
|
|
|
You can't do the camara capture using Servlet or JSP (these things are server side). You will have to do it either in JavaScript or a Java applet or ActiveX component (which wont work in anything other then IE).
For the applet read the following thread on the java forums, which discusses this issue.
|
|
|
|
|
Thanks for reply but JMF is not a free library, besides how to do it through javascript???
|
|
|
|
|
Hello Friends ,
I'm in Final year of computer science .so , i want to developed a project which is based on Real motion of video.
Plz help me ab dat project code.............
I want ready project in which source code in java.]
project title is that.........
" Real-time Motion Trackig and Object Detection in Surveillance Video"
plz send me ready code for this project title...........
ok
bye.......
|
|
|
|
|
Did you read the guidelines above?
Part of # 2 says:
Don't ask "can someone send me the code to create an application that does 'X'.
# 11 says:
If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
Given these two points, you are not going to get what you are asking from this forum. Look around and you will see many replies saying the same thing.
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
public class Sample
{
public static void main(String[] args)
{
System.out.println("Real-time Motion Tracking and Object Detection in Surveillance Video");
}
}
That should get you started.
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
No.
If you want some help here, you're going to ask specific questions and show that you did something on your own. Nobody is going to do your work for you. Besides, if you never learn by yourself, you'll need to be assisted for your future work too.
And oh, please stop using txt spk, this is really annoying.
|
|
|
|
|
Some years ago I ordered just this task to a grant-holder who was also in his last year of computer science and, you know, he did it. So I guess, if he could, you can. Good luck.
|
|
|
|
|
Can anyone help me to build a java Console/SWING application named calculator that evaluates a given expression. Matter of fact this expression should be an input from the user
|
|
|
|
|
There are lots of samples on subjects like this; just try a Google search or go to the Java Tutorials pages.
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
I Want to change foreground color, background color ,and font of text of JTable cell. How can I achieve that .
Trioum
|
|
|
|
|
|
Hello, I have created a do-while loop which is supposed to re-run if the user enters "y" or quit if the user enters "n".
It's about a number guessing game. The user must guess the computer's number and if he chooses to quit or if he guesses correctly, he's given the option to replay.The game is supposed to accept as many guesses are necessary until the number is found but when I run it I'm only able to enter one number. On the second attempt I get the error "Exception in thread "main" java.lang.NullPointerException
at Doloop2.main(Doloop2.java:60)"
Can someone help me out? I would appreciate any help.
I am posting my code hoping that it will make things clearer.
import java.util.*;
public class Doloop2
{
public static void main (String[] args)
{
final int MAX = 100;
int answer, guess, attempt = 0;
String replay = null;
Scanner scan = new Scanner (System.in);
Random generator = new Random();
answer = generator.nextInt(MAX) + 1;
System.out.print("I am thinking of a number "
+ "between 1 and 100. Enter your guess (0 to guit):");
guess = scan.nextInt();
do
{
if (guess > answer)
{
attempt++;
System.out.print ("Try a smaller number (0 to quit): ");
guess = scan.nextInt();
}
if (guess < answer)
{
attempt++;
System.out.print ("Try a bigger number (0 to quit): ");
guess = scan.nextInt();
}
if (guess == answer)
{
attempt++;
System.out.println ("You got it!");
System.out.println ("Number of total attempts:" + attempt);
System.out.println ();
System.out.print("Play again? (y/n)");
replay = scan.nextLine();
}
if (guess == 0)
{
System.out.println ("You ended the game. Play again? (y/n):");
replay = scan.nextLine();
}
}
while (replay.equals("y") || replay.equals("Y"));
}
}
modified on Wednesday, October 20, 2010 11:07 PM
|
|
|
|
|
Hey ur program had a couple of bugs after i removed the null pointer error. Fixed it
Code
-------------------------------------
import java.util.*;
public class Guess
{
public static void main (String[] args)
{
final int MAX = 100;
int answer, guess, attempt = 0;
String replay = "Y";
Scanner scan = new Scanner (System.in);
Random generator = new Random();
answer = generator.nextInt(MAX) + 1;
System.out.print("I am thinking of a number "
+ "between 1 and 100. Enter your guess (0 to guit):");
guess = scan.nextInt();
do
{
if(guess == 101){
answer = generator.nextInt(MAX) + 1;
System.out.print("I am thinking of a number "
+ "between 1 and 100. Enter your guess (0 to guit):");
guess = scan.nextInt();
}
if (guess == 0)
{
System.out.println ("You ended the game. Play again? (y/n):");
replay = scan.next();
guess = 101;
attempt = 0;
}
else{
if (guess > answer)
{
attempt++;
System.out.print ("Try a smaller number (0 to quit): ");
guess = scan.nextInt();
}
if (guess < answer)
{
attempt++;
System.out.print ("Try a bigger number (0 to quit): ");
guess = scan.nextInt();
}
}
if (guess == answer)
{
attempt++;
System.out.println ("You got it!");
System.out.println ("Number of total attempts:" + attempt);
System.out.println ();
System.out.print("Play again? (y/n)");
replay = scan.next();
guess = 101;
attempt = 0;
}
}while (replay.equals("y") || replay.equals("Y"));
}
}
|
|
|
|
|
1. Don't use 'txtspk'.
2. Do use <pre></pre> tags.
Just because questions don't look professional does not mean that answers should follow a similar pattern.
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
Thanks. This works right but could you please explain the logic behind guess = 101 and guess == 101?
|
|
|
|
|
Go back to your basic documentation on assignments and expressions.
guess = 101;
is an assignment statement which sets the variable guess to the value 101.
guess == 101
is an expression which yields the boolean value TRUE if the variable guess contains the value 101, otherwise FALSE .
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
Thanks for your response. I know about the assignment operator (=) and the boolean expression (==). My question should had been why "101"? What's the logic behind "101" in particular? What do we want to achieve by this number?
|
|
|
|
|
Hi guys,
I edited the code a little more yesterday. The logic behind "guess=101" are the following reasons.
1:When the user gives the correct answer or when the user chooses "0" to quit the variable has to be reset to some value, in this case i used "101" but now i ve made a small edit and changed it "guess = MAX+1", just to make the program a little more dynamic.
2:Also, i have included a condition saying if "guess = MAX+1" then the game restarts again prompting the user to guess a number.
Im pasting the edited code below for your reference. I believe now the program is bug clear. I have fun playing
import java.util.*;
public class Guess
{
public static void main (String[] args)
{
final int MAX = 100;
int answer, guess, attempt = 0;
String replay = "Y";
Scanner scan = new Scanner (System.in);
Random generator = new Random();
answer = generator.nextInt(MAX) + 1;
System.out.print("I am thinking of a number "
+ "between 1 and "+ MAX+". Enter your guess (0 to guit):");
guess = scan.nextInt();
do
{
if(guess == MAX + 1){
answer = generator.nextInt(MAX) + 1;
System.out.print("I am thinking of a number "
+ "between 1 and "+ MAX+". Enter your guess (0 to guit):");
guess = scan.nextInt();
}
if (guess == 0)
{
System.out.println ("You ended the game. Play again? (y/n):");
replay = scan.next();
guess = MAX + 1;
attempt = 0;
}
else{
if (guess > answer)
{
attempt++;
System.out.print ("Try a smaller number (0 to quit): ");
guess = scan.nextInt();
}
}
if (guess == 0)
{
System.out.println ("You ended the game. Play again? (y/n):");
replay = scan.next();
guess = MAX + 1;
attempt = 0;
}
else{
if (guess < answer)
{
attempt++;
System.out.print ("Try a bigger number (0 to quit): ");
guess = scan.nextInt();
}
}
if (guess == answer)
{
attempt++;
System.out.println ("You got it!");
System.out.println ("Number of total attempts:" + attempt);
System.out.println ();
System.out.print("Play again? (y/n)");
replay = scan.next();
guess = MAX + 1;
attempt = 0;
}
}while (replay.equals("y") || replay.equals("Y"));
System.out.println("\nThanks for playing - GUESS THE NUMBER");
}
}
|
|
|
|