Java practical memo-1 - Hello this is the best the answers in the qorld please tr it and see our self PDF

Title Java practical memo-1 - Hello this is the best the answers in the qorld please tr it and see our self
Author Jonas Makunyane
Course Informattion Technology
Institution Vaal University of Technology
Pages 8
File Size 143.2 KB
File Type PDF
Total Downloads 103
Total Views 141

Summary

Hello this is the best the answers in the qorld please tr it and see our self...


Description

Question One 1. Write a java program that prompts a user to enter a word, sentence, or numbers. The program should then determine if the word or sentence entered by user is palindrome. (A palindrome is a word, phrase, verse, number, or sentence that reads the same backward or forward but still have the same meaning. Eg: 121, civic) [25]

// palindrome.java import java.util.Scanner; public class palindrome{ private static boolean iterative (String strSubmitted){ int fromLeft; boolean result = false; int fromRight = strSubmitted.length()-1; if (strSubmitted.length()==1) result = true; else { for(fromLeft = 0; fromLeft < strSubmitted.length(); fromLeft ++,fromRight--) { if(strSubmitted.charAt(fromLeft)== strSubmitted.charAt(fromRight)) { result = true; } else result = false; } } return result; } public static void main(String[]args){ boolean is = false; String sentence = ""; Scanner scnr = new Scanner(System.in); System.out.println("Enter a word or sentence:"); sentence = scnr.nextLine(); is = iterative (sentence.toLowerCase()); if (is==true) System.out.println (sentence+" is a palindrome"); else

System.out.println(sentence+" is not a palindrome"); } }

Question two

2. Create a java class called Invoice which has the following attributes:    

Item Number Item Name Unit price Quantity

2.1 Include a constructor which sets the initial values of each attribute to 9999, “none”, 0 and 0 respectively. Provide set methods to set the values of these attributes. Include a method called displayCost() which computes the total cost of the item (quantity * unit price) and displays the item number, name , unit price, quantity and total cost. Save this class as Invoice.java [15] 2.2 Create an application called TestInvoice.java which instantiates an object of the class Invoice. It provides the values of the attributes and displays them including the total cost.Save this application as TestInvoice.java. [10]

// Invoice.java // Creates a class for an Invoice

public class Invoice { privateintitemNumber; private String itemName; privateint quantity; private double price; private double total;

publicintgetItemNumber() { returnitemNumber; }

public void setItemNumber(intnum) { itemNumber = num; } publicintgetQuantity() { return quantity; } public void setQuantity(intnum) { quantity = num;

total = price * quantity; }

public double getPrice() { return price;

}

public void setPrice(double num) { price = num; total = price * quantity; }

public String getItemName() { returnitemName; }

public void setItemName(String name) { itemName = name; }

public void displayLine() {

System.out.println("Item #" + itemNumber + " " + itemName + " Quantity " + quantity + " Each $" + price + " Total $" + total); } } // TestInvoice.java // Tests Invoice class importjava.util.Scanner; public class TestInvoice { public static void main(String[] args) { Invoice a; Invoice b; Invoice c; a = getData(); b = getData(); c = getData(); a.displayLine(); b.displayLine(); c.displayLine(); } public static Invoice getData() { Invoice i = new Invoice(); Scanner input = new Scanner(System.in);

System.out.print("Enter the item number "); i.setItemNumber(input.nextInt()); input.nextLine(); System.out.print("Enter the name of the item "); i.setItemName(input.nextLine()); System.out.print("Enter the price "); i.setPrice(input.nextDouble()); System.out.print("Enter the quantity "); i.setQuantity(input.nextInt()); returni; } }

Question Three

3.1 Develop a Java application that determines the gross pay for an employee. The company pays straight time for the first 40 hours worked by an employee and time and a half (1.5) for all hours worked in excess of 40.

The program should accept positive integers (greater than zero) for the number of hours worked or display the error message (“Sorry you have entered an invalid entry, please try again”).

You’re given the number of hours worked last week and their hourly rates. Your program should input this information, then determine and display the employee’s gross pay. Use class Scanner to input the data. Test Data

Hours worked 40 45 -5

Rate 10 10 15

0

20

Normal Pay R40.00

R475.00 (“Sorry you have entered an invalid entry,please try again”) “Sorry you have entered an invalid entry,please try again”)

//class package definition package salaryemployee; //import Scanner class import java.util.Scanner; //class definition begins here public class SalaryEmployee { //program execution begins here public static void main(String[] args) { //declaration of variables int hrsWorked;

//input

double rate = 0;

//input

double normalPay = 0;

//output

double overTimePay = 0;

//output

Scanner input = new Scanner(System.in);

//other variable

System.out.print("Please enter the number of hours worked\t"); hrsWorked = input.nextInt(); System.out.print("Please enter the hourly rate\tR"); rate = input.nextDouble(); //program logic normalPay = hrsWorked * rate;

OverTime

if( hrsWorked > 40 ) { double weeklyPay = 40 * rate; overTimePay = ((hrsWorked - 40) * rate * 1.5) + weeklyPay; System.out.println("The overtime pay is :\tR" + overTimePay); }//end if else if(hrsWorked > 0 && hrsWorked...


Similar Free PDFs