10 Replies - 1185 Views - Last Post: 29 June 2008 - 11:23 PM Rate Topic: -----

#1 kill99   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 03-May 08

Need help to shorten the code

Posted 23 June 2008 - 09:36 PM

import java.io.*;
import java.lang.Math.*;
class task1
{

	public static void main(String args[])throws Exception
	{
		BufferedReader input=
		new BufferedReader(new InputStreamReader(System.in));

		// our double
		double a = 0.0;
		double b = 0.0;
		double c = 0.0;
		// a boolean on which we will loop
		boolean correct = false;
		do {
		   System.out.print("a: ");
		   String inputStr = input.readLine();
		   try {
			   a = Double.parseDouble(inputStr);
			   // parse worked flag to end loop
			   correct = true;
		   }
			 catch(NumberFormatException e)
				{
					System.out.println("Please enter a number");
		   		}


		   System.out.print("b: ");
		   inputStr = input.readLine();
		   try {
			   b = Double.parseDouble(inputStr);
			   // parse worked flag to end loop
			   correct = true;
		   }
			 catch(NumberFormatException e)
					   {
							System.out.println("Please enter a number");
		   			   }

		   System.out.print("c: ");
		   inputStr = input.readLine();
		   try {
			   c = Double.parseDouble(inputStr);
			   // parse worked flag to end loop
			   correct = true;
		   }

		   catch(NumberFormatException e)
		   {
				System.out.println("Please enter a number");
		   }
} while(!correct);

double temp=Math.sqrt((b*b)-(4*a*c));
double temp2=(-b+temp)/2*a;
System.out.println("The first solution is "+temp2);
double temp1=Math.sqrt((b*b)-(4*a*c));
double temp3=(-b-temp1)/2*a;
System.out.println("The second solution is "+temp3);
	}
}



My program runs when user input value for a,b and c

when i input a word in example a = b it will prompt "please enter a numbe"r afterward it will skip A and move to b. So how do i write out a code to reprint it to made sure when user after enter a word it will go back to A and ask ask user to enter a number hope i did no make ur blur

example:

a = 2
b= b
Please enter a number
"then it jump to c and will not go back to b to enter a number"
c= -4
The First solution is .....
the Second solution is ......




1 last questions

import java.io.*;

// starting point of program
class smooth
{
  // starting point of application
   public static void main(String[] args)
   {

	   int[] signal  = {1, 5, 4, 5, 7, 6, 8, 6, 5, 4, 5, 4};

	   int[] smooth = makeItSmooth(signal);

	   for(int i = 0;i<smooth.length;i++){
		   System.out.print(" "+smooth[i]);
	   }

	}
	public static int[] makeItSmooth(int[] signal){
		int[] result = new int[signal.length];

		for(int i =0;i<signal.length;i++){
			if(i==0){
				result[0] = (signal[0]+signal[1])/2;
			}
			if(i==signal.length-1){
				result[signal.length-1]=(signal[signal.length-1]+signal[signal.length-2])/2;
				break;
			}
			if(i>0){
				result[i] = (signal[i]+signal[i+1]+signal[i-1])/3;
			}
		}
		return result;
	}


}



is there any way i can shorten the code?

Is This A Good Question/Topic? 0
  • +

Replies To: Need help to shorten the code

#2 mensahero   User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 680
  • Joined: 26-May 08

Re: Need help to shorten the code

Posted 24 June 2008 - 12:47 AM

while(!correct){
		  System.out.print("a: ");
		   String inputStr = input.readLine();
		   try {
			   a = Double.parseDouble(inputStr);
			   // parse worked flag to end loop
			   correct = true;
		   }
			 catch(NumberFormatException e)
				{
					System.out.println("Please enter a number");
				   }
}

correct = false;

while(!correct){
		   System.out.print("b: ");
		   inputStr = input.readLine();
		   try {
			   b = Double.parseDouble(inputStr);
			   // parse worked flag to end loop
			   correct = true;
		   }
			 catch(NumberFormatException e)
					   {
							System.out.println("Please enter a number");
						  }
}

correct = false;
while(!correct){
		   System.out.print("c: ");
		   inputStr = input.readLine();
		   try {
			   c = Double.parseDouble(inputStr);
			   // parse worked flag to end loop
			   correct = true;
		   }

		   catch(NumberFormatException e)
		   {
				System.out.println("Please enter a number");
		   }
}





Use individual while loop instead of a single Do While Loop. The code above is not tested, It just meant to show an idea. Correct me if i'm wrong.
Was This Post Helpful? 0
  • +
  • -

#3 kill99   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 03-May 08

Re: Need help to shorten the code

Posted 26 June 2008 - 10:30 PM

ok for the task i have the problem and thanks for the help


now i have another question to ask

about object orientation

this my code

 
class Account
{
	double balance;
	String name;

	public Account(double bal, String id)
	{
		balance = bal;
		name = id;
	}
	public void display()
	{
		System.out.println("Name = " + name);
		System.out.println("Balance = " + balance);
	}
	public void deposit(double amount)
	{
		balance = balance + amount;
	}

}
public class UseAccount
{
	public static void main(String args[])
	{
		Account x = new Account("john" , 150);
		x.display();
	}
}



i keep getting this error

C:\Documents and Settings\user\Desktop\java\UseAccount.java:26: cannot find symbol
symbol : constructor Account(java.lang.String,int)
location: class Account
Account x = new Account("john" , 150);
^
1 error

Tool completed with exit code 1


an1 can tell me wat i did wrong??
Was This Post Helpful? 0
  • +
  • -

#4 RoboAlex   User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 39
  • Joined: 26-June 08

Re: Need help to shorten the code

Posted 26 June 2008 - 10:49 PM

I think you're problem is with having two classes in one file. Your UseAccount class isn't used for anything but containing main, so you should just copy main over to Account and delete the UseAccount class.

This post has been edited by RoboAlex: 26 June 2008 - 10:50 PM

Was This Post Helpful? 0
  • +
  • -

#5 kill99   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 03-May 08

Re: Need help to shorten the code

Posted 26 June 2008 - 11:11 PM

View PostRoboAlex, on 26 Jun, 2008 - 10:49 PM, said:

I think you're problem is with having two classes in one file. Your UseAccount class isn't used for anything but containing main, so you should just copy main over to Account and delete the UseAccount class.



ok i solve the things is no the class file area

public Account(double bal, String id)
	{
		balance = bal;
		name = id;

	}


since i put double as the first and string as the second

i should put the code as for the bottom area

public static void main(String args[])
	{
		Account x = new Account( 150, "john");
		x.display();
	}


Was This Post Helpful? 0
  • +
  • -

#6 kill99   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 03-May 08

Re: Need help to shorten the code

Posted 27 June 2008 - 04:27 PM

Hi all need help again i trying to do up this example

*****
****
***
**
*

class for3
{
public static void main(String arg[])
	{
		for(int col = 4; col >= 1 ; col--)
		{
			for(int row = 1; row > col; row--)
			{
				System.out.print("*");
			}
				System.out.println();
		}
	}
}



an1 people can tell what mistake i have done
Was This Post Helpful? 0
  • +
  • -

#7 RoboAlex   User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 39
  • Joined: 26-June 08

Re: Need help to shorten the code

Posted 27 June 2008 - 04:55 PM

The decrementing for loops confused me a bit, I prefer incrementing loops unless I have a specific reason, but it's not a big problem...

Anyway, your problem is with the inner loop. row starts out with the value 1, and goes down until it becomes less than or equal to col. Col is never less than one, so row is ALWAYS less than or equal to col, so the program will NEVER enter the internal loop. Also, your outside loop will only go 4 times: col = 4,3,2,1 and when col = 0 it will no longer satisfy col >= 1 and leave the loop. Hopefully you can figure it out from there, I don't want to just give you the code, but II really think you should be able to figure it out from there.
Was This Post Helpful? 0
  • +
  • -

#8 kill99   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 03-May 08

Re: Need help to shorten the code

Posted 28 June 2008 - 08:12 PM

class for3
{
public static void main(String arg[])
	{
		for(int row = 5; row <= 1; row--)
		{
			for(int col = 1; col <= row ; col++)
			{
				System.out.print("*");
			}
				System.out.println();
		}
	}
}



hmm still can't solve it
Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Need help to shorten the code

Posted 28 June 2008 - 08:43 PM

Your constructor of account is

 
class Account
{
	public Account(double bal, String id)
	{
		balance = bal;
		name = id;
	}



You inversed the parameters trying to create a new Account

public class UseAccount
{
	public static void main(String args[])
	{
		Account x = new Account("john" , 150);	// <-------------------------------------
		x.display();
	}
}


Was This Post Helpful? 0
  • +
  • -

#10 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Need help to shorten the code

Posted 28 June 2008 - 08:53 PM

   for(int row = 5; row <= 1; row--)



Lets row have the value 5
While row is <= 1
You'll never enter the loop

you want

	public static void main(String arg[])
	{
		for(int row = 5; row >= 1; row--)   // <-------- instead of <=
		{
			for(int col = 1; col <= row; col++)
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}


Was This Post Helpful? 0
  • +
  • -

#11 kill99   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 03-May 08

Re: Need help to shorten the code

Posted 29 June 2008 - 11:23 PM

ok thanks you for the help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1