Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author Melanie
- * This is the LabExample class - used to explore if and loop statements
- *
- */
- public class LabExample {
- public int highestOfTwo(int a,int b){
- if (a>b) {
- return a;
- }else if(a<b) {
- return b;
- }else{
- return -1;
- }
- }
- public String calculateGrade(int score) {
- if (score>=0 && score<40)
- {
- return "Fail";
- }else if(score>=40 && score<50){
- return "3rd";
- }else if(score>=50 && score <60) {
- return "2ii";
- }else if(score>=60 && score<70) {
- return "2i";
- }else if(score>=70 && score<=100) {
- return "1st";
- }else {
- return "Invalid mark";
- }
- }
- public String headsOrTails(String guess) {
- String answer="";
- int random=(int)(Math.random() * ((1 - 0) + 1)) + 0;
- if (random==1) {
- answer="heads";
- }else {
- answer="tails";
- }
- if (guess.equals(answer)){
- return "Correct: you guessed "+guess+" and I flipped "+answer+"";
- }else {
- return "Incorrect: you guessed "+guess+" and I flipped "+answer+"";
- }
- }
- public int sumFromOneToWhat(int top) {
- top=(top*(top+1))/2;
- return top;
- }
- public int sumFromWhatToWhat(int bottom,int top) {
- int sum=0;
- for(int i=bottom;i<=top;i++){
- sum+=i;
- }
- return sum;
- }
- }
- /////////////////////////////////////////////
- /**
- *
- * @author Melanie
- * This is the Second Salary class - used to explore if and loop statements
- *
- */
- public class Salary {
- public double salaryTax(double salary) {
- if(salary>=45000) {
- return salary*0.5;
- }else if(salary>=30000 && salary<45000){
- return salary*0.7;
- }else {
- return salary*0.85;
- }
- }
- public double calculateNI(double gross,char NI) {
- switch(NI) {
- case 'A':
- gross=gross*0.88;
- break;
- case 'B':
- gross=gross*0.9415;
- break;
- case 'C':
- gross=gross*0.98;
- break;
- case 'a':
- gross=gross*0.88;
- break;
- case 'b':
- gross=gross*0.9415;
- break;
- case 'c':
- gross=gross*0.98;
- break;
- default:
- gross=gross;
- }
- return gross;
- }
- public double salaryTotal(double salries[]) {
- double sum=0;
- for(double i : salries) {
- sum+=i;
- }
- return sum;
- }
- public double salaryAverage(double salries[]) {
- double avr=0;
- for(double i : salries) {
- avr+=i;
- }
- return avr/salries.length;
- }
- public double[] salaryIncrease(double salaries[]) {
- for(int i=0;i<salaries.length;i++) {
- salaries[i]=salaries[i]+salaries[i]*0.05;
- }
- return salaries;
- }
- }
- ////////////////////////////////////////////////////
- /**
- *
- * @author Melanie
- * This is the WordPlay class - used to explore if and loop statements
- *
- */
- public class WordPlay {
- public String whatComesFirst(String an1,String an2) {
- int ans=an1.compareToIgnoreCase(an2);
- String answer="";
- if (ans==0) {
- answer=""+an1+" is the same as "+an2+"";
- }else if (ans>0) {
- answer=""+an2+" comes before "+an1+" in the alphabet";
- }else if(ans<0) {
- answer=""+an1+" comes before "+an2+" in the alphabet";
- }
- return answer;
- }
- public String backwardsString(String name) {
- StringBuilder input1 = new StringBuilder();
- // append a string into StringBuilder input1
- input1.append(name);
- // reverse StringBuilder input1
- input1 = input1.reverse();
- return input1.toString();
- }
- public String[] addressBook(String names[],String numbers[]) {
- for(int i=0;i<names.length;i++) {
- names[i]=names[i]+" "+numbers[i];
- }
- return names;
- }
- public String rockPaperScissors(String player) {
- String CPU="";
- int random=(int)(Math.random() * ((2 - 0) + 1)) + 0;
- if(random==0) {
- CPU="rock";
- }else if(random==1) {
- CPU="paper";
- }else if(random==2) {
- CPU="scissors";
- }
- int temp=CPU.compareToIgnoreCase(player);
- if(temp==0) {
- CPU="USER:"+player+" vs COMP:"+CPU+" it is a draw";
- }
- if(temp<0) {
- CPU="USER:"+player+" vs COMP:"+CPU+" COMPUTER wins";
- }
- if(temp>0) {
- CPU="USER:"+player+" vs COMP:"+CPU+" user wins";
- }
- System.out.println(CPU);
- return CPU;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement