Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Java for beginner

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java for beginner

    Hi im a beginner in java , i have my answer to this one but i am not satisfied with my answer can you help me to find other answers for this?


    1.XYZ Company gives year-end bonuses to its employees based on their number of years of service and their salary using the following: Years of service=1 bonus=10% of salary; years of service=2 to 5 bonus=20% of salary; years of service=6 to 10 bonus=50% of salary; years of service= 11 and above bonus=75%

    2.write a program that asks the user for the hours worked for the week and the hourly rate. The basic salary is computed as Salary=hours worked X hourly rate.... Bonuses are given: No. of hours >45 bonus=500 pesos; No. of hours> 40 and <=45 bonus=250 pesos; No. of hours>35 and <=40 bonus=150... Display the basic salary, bonus and the total salary (basic salary + bonus)


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java for beginner

    but i am not satisfied...can you help me to find other answers for this?
    Post your answer and ask a specific question - and elaborate as to why you are not satisfied. That's a better learning exercise than asking for 'other' answers, which I'm sorry to say you won't get

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java for beginner

    Welcome to the Forum! Please read this topic to learn how to post your code correctly along with other useful info for newcomers.

  4. #4
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hi people , here's my code to the problem but my prof said there is another way to solve this , can you help me thanks , i provided my answer below

    1.XYZ Company gives year-end bonuses to its employees based on their number of years of service and their salary using the following: Years of service=1 bonus=10% of salary; years of service=2 to 5 bonus=20% of salary; years of service=6 to 10 bonus=50% of salary; years of service= 11 and above bonus=75%


    package yearendbonus;

    import java.util.Scanner;

    public class YearEndBonus
    {

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

    System.out.println("Years in service: ");
    double years = sc.nextDouble();
    System.out.println("Salary: ");
    double salary = sc.nextDouble();

    if(years == 1){
    System.out.println("Bonus = "+ (salary*.10));
    }
    else if((years >=2)&&(years<=5)){
    System.out.println("Bonus = "+ (salary*.20));
    }
    else if((years >=6)&&(years<=10)){
    System.out.println("Bonus = "+ (salary*.50));
    }
    else if(years >=11){
    System.out.println("Bonus = "+ (salary*.75));
    }

    }
    }


    2.Write a program that asks the user for the hours worked for the week and the hourly rate. The basic salary is computed as Salary=hours worked X hourly rate.... Bonuses are given: No. of hours >45 bonus=500 pesos; No. of hours> 40 and <=45 bonus=250 pesos; No. of hours>35 and <=40 bonus=150... Display the basic salary, bonus and the total salary (basic salary + bonus)


    package computesalary;

    import java.util.Scanner;

    public class ComputeSalary {

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

    System.out.println("Hours of work: ");
    double hours = sc.nextDouble();

    System.out.println("Hourly rate: ");
    double rate = sc.nextDouble();

    double basicsalary = hours*rate;
    System.out.println("Basic Salary = "+basicsalary);

    if((hours > 35)&&(hours <= 40)){
    System.out.println("Bonus = 150");
    System.out.println("Total Salary = "+ (basicsalary+150));
    }
    else if((hours > 40)&&(hours <= 45)){
    System.out.println("Bonus = 250");
    System.out.println("Total Salary = "+ (basicsalary+250));
    }
    else if(hours > 45){
    System.out.println("Bonus = 500");
    System.out.println("Total Salary = "+ (basicsalary+500));
    }

    }
    }

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java for beginner

    I have merged your new thread with your old. Please read the link posted by Greg in his post above for forum guidelines, including how to properly format code. I would also recommend as I mentioned in my post above asking a question, or explaining what is wrong as clearly as possible.

  6. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java for beginner

    1.XYZ Company gives year-end bonuses to its employees based on their number of years of service and their salary using the following: Years of service=1 bonus=10% of salary; years of service=2 to 5 bonus=20% of salary; years of service=6 to 10 bonus=50% of salary; years of service= 11 and above bonus=75%

    package yearendbonus;
     
    import java.util.Scanner;
     
    public class YearEndBonus 
    {
     
    public static void main(String[] args) 
    {
    Scanner sc = new Scanner (System.in);
     
    System.out.println("Years in service: ");
    double years = sc.nextDouble();
    System.out.println("Salary: ");
    double salary = sc.nextDouble();
     
    if(years == 1){
    System.out.println("Bonus = "+ (salary*.10));
    }
    else if((years >=2)&&(years<=5)){
    System.out.println("Bonus = "+ (salary*.20));
    }
    else if((years >=6)&&(years<=10)){
    System.out.println("Bonus = "+ (salary*.50));
    }
    else if(years >=11){
    System.out.println("Bonus = "+ (salary*.75));
    }
     
    }
    }

    2.Write a program that asks the user for the hours worked for the week and the hourly rate. The basic salary is computed as Salary=hours worked X hourly rate.... Bonuses are given: No. of hours >45 bonus=500 pesos; No. of hours> 40 and <=45 bonus=250 pesos; No. of hours>35 and <=40 bonus=150... Display the basic salary, bonus and the total salary (basic salary + bonus)

    package computesalary;
     
    import java.util.Scanner;
     
    public class ComputeSalary {
     
    public static void main(String[] args) {
    Scanner sc = new Scanner (System.in);
     
    System.out.println("Hours of work: ");
    double hours = sc.nextDouble();
     
    System.out.println("Hourly rate: ");
    double rate = sc.nextDouble();
     
    double basicsalary = hours*rate;
    System.out.println("Basic Salary = "+basicsalary);
     
    if((hours > 35)&&(hours <= 40)){
    System.out.println("Bonus = 150");
    System.out.println("Total Salary = "+ (basicsalary+150));
    }
    else if((hours > 40)&&(hours <= 45)){
    System.out.println("Bonus = 250");
    System.out.println("Total Salary = "+ (basicsalary+250));
    }
    else if(hours > 45){
    System.out.println("Bonus = 500");
    System.out.println("Total Salary = "+ (basicsalary+500));
    }
     
    }
    }


    There's no problem with this code , actually it is running , i want your suggestion on what is the other way or other solution in which i can get same results but different codes..thanks

  7. #7
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Java for beginner

    Hi,

    I format your code like this,

    import java.util.Scanner;
     
    public class YearEndBonus  {
     
    	private double years;
    	private double salary;
    	public double bonus;
    	public YearEndBonus (double years,double salary)
    	{
    		this.years=years;
    		this.salary=salary;
     
    	}
    	public double busLogic()
    	{
    		if(years == 1){
    			bonus=salary*.10;
    			}
    			else if((years >=2)&&(years<=5)){
    			bonus=salary*.20;
    			}
    			else if((years >=6)&&(years<=10)){
    				bonus=salary*.50;
    			}
    			else if(years >=11){
    				bonus=salary*.75;
    			}
     
    		return bonus;
     
    	}
    	public void showData()
    	{
    		System.out.println("Bonus = "+ busLogic());
     
    	}
     
    	public static void main(String[] args) 
    	{
    	Scanner sc = new Scanner (System.in);
     
    	System.out.println("Years in service: ");
    	double years = sc.nextDouble();
    	System.out.println("Salary: ");
    	double salary = sc.nextDouble();
     
    	YearEndBonus  yrBonus=new YearEndBonus (years,salary);
    	yrBonus.busLogic();
    	yrBonus.showData();
    	}
     
     
    }

    You can also use getter and setter methods to get the input that is upto you. But you can use methods, it is useful for resusing the code whenever we want the same functionality.


    If any clarification please post here

Similar Threads

  1. Java Beginner
    By shyamal.punekar in forum Member Introductions
    Replies: 1
    Last Post: January 22nd, 2014, 12:39 AM
  2. Beginner Java HELP!
    By finals88 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 30th, 2013, 01:14 AM
  3. Replies: 2
    Last Post: January 18th, 2013, 11:12 AM
  4. Replies: 1
    Last Post: January 6th, 2013, 06:32 AM
  5. Java Beginner
    By rannoune in forum Java Theory & Questions
    Replies: 3
    Last Post: December 25th, 2009, 03:30 AM