PDA

View Full Version : Need help on Java coding, especially method calls



Geforce4ti4200
04-16-2005, 08:27 PM
Hello all, I am practicing more Java but the part that still gets to me is method calls. They are just so complicated and confusing :( I also dont quite know how to complete some parts of the coding.




import java.util.Scanner;//imports scanner so the user can enter data

public class CompoundInt
{

public static void main(String args[])
{

Scanner input = new Scanner(System.in);//allows user to input data

double amount=10;//define deposit amount
double prin=1000;//your balance before interest
double rate=.05;//the interest rate input by the user
double year=7;

System.out.print( "Please enter the rate " );
rate = input.nextDouble();//advances to the next prompt

System.out.printf("%s%20s\n","Year", "Amount");//display headers

rate=findRate(amount,prin,rate,year);//set up a call method for the declared names
System.out.printf("%4f%,20.2f\n",year, amount);//display your balance


}//end main
static double findRate(double amount,double prin,double year, double rate){//calls the declared method
//calculate here to find Rate
double total = 0;
for(int year2=1;year2<=year;year2++) {//loop to calculate interest earned over a decade
// total = fill in this code
total=prin*Math.pow(1+rate,year);//calculates the amount for each year
}
return(total);
}//end callMethod
}//end file


Problem? It always shows
Year Amount
7.000000 10.00

no matter what I do. Either I am missing some code or the program doesnt know how to run my code. There are no errors, but it should be showing the amount for each year......


import java.util.Scanner;//imports scanner so the user can enter data

public class ThreeA
{

public static void main(String args[])
{

Scanner input = new Scanner(System.in);//allows user to input data



char payType;//declare character variable "payType" which may be of three types: s, c or other.
float rate1, rate2;//declare two variable types of rates: commission (rate1) and hourly (rate2)
float sales;//declare variable "sales" which is the total dollar amont of products sold, applicable for commission employees only
int hours;//declare variable "hours" which is the duration that employee worked, applicable for salary employees only
float earning;//declare variable "earning" which is the dollar amount that the employee earns


System.out.print( "Please enter the pay type (c for commission, s for hourly, x for neither) " );
//Note: I do not know the proper code for inputing a charcter to be assigned to "payType"
//Therefore, the following line needs to be corrected. "input.nextLine" is not appropriate for inputing charcters
payType = input.nextLine();//advances to the next prompt




//set up a call method for the declared names
payRoll(payType, rate1, rate2, sales, hours, earning);

}//of main

//calls the declared method
static void payRoll(char payType, float rate1, float rate2, float sales, int hours, float earning) {
if (payType== 'c')
{//start if
System.out.print( "Please enter the commision rate" );
rate1 = input.nextFloat();//advances to the next prompt
System.out.print( "Please enter how much you sold" );
sales = input.nextFloat();//advances to the next prompt
earning=rate1*sales;
System.out.printf("%f\n","You earned a commission of $ ", earning);
}//end if

else

if (payType== 's')
{//start if
System.out.print( "Please enter the hourly rate" );
rate2 = input.nextFloat();//advances to the next prompt
System.out.print( "Please enter the number of hours worked" );
hours = input.nextInt();//advances to the next prompt
earning=rate2*hours;
System.out.printf("%f\n","You earned a salary of $ ", earning);
}//end if

else

System.out.print( "You earned nothing" );

}//of call method
}//end program


problem? The instructor started teaching me that to call a method for this, I need to put most of the code below this-----> }//of main but I have no clue how to call method for something this complicated. I can get it to work but without call method.


//takes int minutes and returns float cost
//$5 per 15 min plus $10 starting cost, $5 discount if >180 minute

import java.util.Scanner; //class scanner
public class PianoLesson
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);//allows user to input data

int minute;
float cost;

System.out.print( "How long did your piano lessons last? " );
minute = input.nextInt();//advances to the next prompt
cost=findCost(minute,cost);//set up a call method for the declared names
System.out.printf("your total cost is $%.2f\n" ,cost);

}//of main

static float findCost(int minute, float cost){
cost=0;//says variable cost isnt initalized but it is!!!!!!
cost=minute*1/3+10;
if (minute>180)//checks if your lesson lasts over 180 minute
cost=cost-5;//grants a $5 discount
return(cost);

}//of method
}// of file

problem? There shouldnt be any, it complains variable cost might not have been initialized but I did that! I dont know if anyone knows whats going on with this one but if you do, please tell! Ive spent hours on this, im baffled whats the malfunction with my compiler at this point!


//Five proucts are sold: 1=$2.98, 2=$4.50, 3=$9.98, 4=$4.49, 5=$6.87
//program for product number and quantity sold. Use switch stetements
//to determine retail price for each product and calculate total price

import java.util.Scanner;//imports scanner so the user can enter data

public class FiveProducts
{

public void main(String args[])
{

Scanner input = new Scanner(System.in);//allows user to input data

int product;//user types in number of product sold
int quantity;//specifies how many of that kind to be sold
int total;//counts up the amount of items
int count1;//counts item 1 which costs $2.98
int count2;//counts item 2 which costs $4.50
int count3;//counts item 3 which costs $9.98
int count4;//counts item 4 which costs $4.49
int count5;//counts item 5 which costs $6.87



System.out.print( "Please enter the product number " );
System.out.print( "Please enter quantity of product " );

while(input.hasNext())
{
product=input.nextInt();
quantity=input.nextInt();
total+=quantity;//counts up amount of item
++quantityCounter;//increment number of items
}//end while
}//end method inputitems

public void incrementQuantity(int Q)
{

switch(product/5)//determines which of the 5 products user bought
{
case 1:
++count1;//increment product 1
break;//exit switch

case 2:
++count2;
break;

case 3:
++count3;
break;

case 4:
++count4;
break;

case 5:
++count5;
break;


System.out.printf("Your total for the products purchased are",quantity);

}//end switch
}
}

problem? It cant find symbol variables but accroding to the book, I did exactly as instructed. This one also baffles me. I know the program is almost complete but those errors are uncalled for!


This is four older programs that I am going back to add method calls and make them work as best as I can on the user's part. I cant help it if my compilier has any malfunctions or doesnt understand supposedly correct code. If any of you experts however can point out any errors on my part, do let me know. I still have alot to learn and feel I could improve on those complex programs.

hchu
04-16-2005, 10:09 PM
why would you expect the first program to print the amount for each year if it isn't even in a loop? also you don't seem to be doing anything with the variable 'amount'?

Stang_Man
04-16-2005, 11:17 PM
wrong forum...

bldegle2
04-17-2005, 03:45 AM
this is certainly impressive. :slobber: at least i am impressed, don't know about anyone else. :rolleyes:

R U taking a class at University? If so, I am sure they have study groups just for what you want. :slap:

and i am sure there are Java forums out there in the nether world, and i will bet an answer would be immediately forthcoming with a short visit.

baldy :D

Geforce4ti4200
04-17-2005, 06:31 AM
why would you expect the first program to print the amount for each year if it isn't even in a loop? also you don't seem to be doing anything with the variable 'amount'?


Its here!


for(int year2=1;year2<=year;year2++) {//loop to calculate interest earned over a decade

I am not changing it, the instructor suggest this one. If anyone else knows why its not compiling correctly, let me know. I cant see any errors for the life of me :stick: as for java message boards, do link me

saratoga
04-18-2005, 04:38 PM
I'm going to go out on a limb and guess that this:


for(int year2=1;year2<=year;year2++) {//loop to calculate interest earned over a decade
// total = fill in this code
total=prin*Math.pow(1+rate,year);//calculates the amount for each year
}

should be this:


for(int year2=1;year2<=year;year2++)
total+=prin*Math.pow(1+rate,year);
}

(I added the plus)

Since otherwise you're not actually summing, just computing each ammount and then discarding it. I could totally be misunderstanding what that function does though.

bh2k
05-05-2005, 07:40 PM
use the [.code][./code] tags dude, that is so obnoxious to read...