MyProgrammingLab 6.2,6.3,6,4, MPL

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Given the integer variables x, y, and z, write a fragment of code that assigns the smallest of x, y, and z to another integer variable min. Assume that all the variables have already been declared and that x, y, and z have been assigned values.

if(x < y){ if(x<z){ min = x; }else{ min = z; } }else{ if(y<z){ min = y; }else{ min = z; } }

printErrorDescription is a method that accepts one int argument and returns no value. Write a statement that invokes the method printErrorDescription, passing it the value 14. Assume that printErrorDescription is defined in the same class that calls it.

printErrorDescription ( 14 ) ;

Write the code for invoking a method named sendSignal. There are no arguments for this method. Assume that sendSignal is defined in the same class that calls it.

sendSignal();

Write a method, makeEmailAddress, that is passed two String arguments: the first is a username, like "leadbelly" and the second is a domain name, like "blues.com". The method returns an email address formed from joining these with an "@": "[email protected]".

public String makeEmailAddress (String aString, String bString) { return aString + "@" + bString; }

toThePowerOf is a method that accepts two int arguments and returns the value of the first parameter raised to the power of the second. An int variable cubeSide has already been declared and initialized. Another int variable, cubeVolume, has already been declared. Write a statement that calls toThePowerOf to compute the value of cubeSide raised to the power of 3 and that stores this value in cubeVolume. Assume that toThePowerOf is defined in the same class that calls it.

cubeVolume = toThePowerOf ( cubeSide , 3 ) ;

Given that a method receives three parameters a, b, c, of type double, write some code, to be included as part of the method, that checks to see if the value of a is 0; if it is, the code prints the message "no solution for a=0" and returns from the method.

if(a == 0) { System.out.print("no solution for a=0"); }

Write the definition of a method min that has two int parameters and returns the smaller.

int min(int num1, int num2) { if(num1 < num2) return num1; else return num2; }

Assume that x is a variable that has been declared as an int and been given a value. Assume that oneMore is a function that receives a single integer parameter and returns a value that is one greater than its argument. (So if you pass 7 to oneMore it will return 8. Thus, the expression oneMore(9) has the value 10. Write an expression whose value is four more than x without using the standard Java arithmetic operators (*,+, etc.). Instead, use calls to oneMore to accomplish this. In this exercise you must write this as a single expression-- you must not write any statements. Also, you may only use the oneMore function-- no other functions or operators.

oneMore(oneMore(oneMore(oneMore(x))))

printStars is a method that accepts a single int argument and returns no value. It prints as many stars as indicated by its argument. Given a variable x of type double that has been given a value, write a statement that invokes printStars passing x as an argument. Assume that printStars is defined in the same class that calls it.

printStars(x);

Write a method, getEmailDomain, that is passed a String argument that is an email address and returns the domain name part. So if "[email protected]" is passed, the method returns "salzberg.de". Assume that the argument will always be a correctly formatted email address.

public String getEmailDomain (String aString) { return aString.substring(aString.indexOf("@") + 1, aString.length()); }

Write a method, getEmailUserName, that is passed a String argument that is an email address and returns the user-name part. So if "[email protected]" is passed, the method returns "mozart". Assume that the argument will always be a correctly formatted email address.

public String getEmailUserName (String aString) { return aString.substring(0, aString.indexOf("@")); }

Write a method, getFirstLine, that is passed a String argument and that returns the first line. (Recall that lines are terminated with the "\n" character.) Assume that the argument contains at least one complete, newline-terminated line.

public String getFirstLine (String aString) { return aString.substring(0, aString.indexOf("\n")); }

Write a method, getSecondLine, that is passed a String argument and that returns the second line, without its newline character. (Recall that lines are terminated with the "\n" character.) Assume that the argument contains at least two complete, newline-terminated lines.

public String getSecondLine (String aString) { return aString.substring(aString.indexOf("\n") + 1, aString.indexOf("\n") + 1 + aString.substring(aString.indexOf("\n") + 1, aString.length()).indexOf("\n")); }

Write a method, makeSubjectLine, that gets a String argument and returns the same String but with "Subject: " in front of it. So if the argument is "Are you going to the show?" the method returns "Subject: Are you going to the show?".

public String makeSubjectLine(String text) { return "Subject: " + text; }

Write a method min that has three string parameters and returns the smallest.

public String min (String aString, String bString, String cString) { if (aString.compareTo(bString) < 0 && aString.compareTo(cString) < 0) { return aString; } else if (bString.compareTo(cString) < 0 && bString.compareTo(aString) < 0) { return bString; } else { return cString; } }

Write the definition of a method powerTo, which receives two parameters. The first is a double and the second is an int. The method returns a double. If the second parameter is negative, the method returns zero. Otherwise it returns the value of the first parameter raised to the power of the second parameter.

public double powerTo (double adouble, int aint) { if (aint < 0) { return 0.0; } else { return Math.pow(adouble, (double) aint); } }

Write a static method, getBigWords, that gets a single String parameter and returns an array whose elements are the words in the parameter that contain more than 5 letters. (A word is defined as a contiguous sequence of letters.) EXAMPLE: So, if the String argument passed to the method was "There are 87,000,000 people in Canada", getBigWords would return an array of two elements, "people" and "Canada". ANOTHER EXAMPLE: If the String argument passed to the method was "Send the request to [email protected]", getBigWords would return an array of three elements, "request", "support" and "turingscraft".

public static String[] getBigWords(String sentence) { int totalbigWords, currentWordStart, currentWordEnd, pointer; totalbigWords = currentWordStart = currentWordEnd = pointer = 0; while ((pointer != sentence.length() - 1) && (!sentence.isEmpty())) { if(Character.isLetter(sentence.charAt(pointer))) { currentWordStart = pointer; while ((Character.isLetter(sentence.charAt(pointer))) && (pointer < sentence.length() - 1)) { pointer++; } currentWordEnd = pointer; if (currentWordEnd == sentence.length() - 1) currentWordEnd++; if (currentWordEnd == 0) currentWordEnd++; if (currentWordEnd - currentWordStart > 5) { totalbigWords++; currentWordStart = currentWordEnd = pointer; } } else pointer++; } String[] bigWords = new String[totalbigWords]; totalbigWords = currentWordStart = currentWordEnd = pointer = 0; while (pointer < sentence.length() - 1) { if(Character.isLetter(sentence.charAt(pointer))) { currentWordStart = pointer; while ((Character.isLetter(sentence.charAt(pointer))) && (pointer < sentence.length() - 1 )) { pointer++; } currentWordEnd = pointer; if (currentWordEnd == 0) currentWordEnd++; if (currentWordEnd == sentence.length() - 1) currentWordEnd++; if (currentWordEnd - currentWordStart > 5) { bigWords[totalbigWords] = sentence.substring(currentWordStart, currentWordEnd); totalbigWords++; currentWordStart = currentWordEnd = pointer; } } else pointer++; } return bigWords; }

Write a method, isEmailAddress, that is passed a String argument. It returns true or false depending on whether the argument has the form of an email address. In this exercise, assume that an email address has one and only one "@" sign and no spaces, tabs or newline characters.

public static boolean isEmailAddress (String aString) { if (aString.indexOf("@") != -1) { if (aString.indexOf("@") == aString.lastIndexOf("@")) { if (aString.indexOf(" ") == -1) { if (aString.indexOf("\n") == -1) { if (aString.indexOf("\t") == -1) { return true; } } } } } return false; }

Write the definition of a method twice, which receives an integer parameter and returns an integer that is twice the value of the parameter.

public static int twice(int a) { int b = 2 * a; return b; }

Write the definition of a method printAttitude, which has an int parameter and returns nothing. The method prints a message to standard output depending on the value of its parameter. If the parameter equals 1, the method prints disagree If the parameter equals 2, the method prints no opinion If the parameter equals 3, the method prints agree In the case of other values, the method does nothing. Each message is printed on a line by itself.

public static void printAttitude ( int x ){ if ( x == 1 ){ System.out.println ( "disagree" ) ; } if ( x == 2 ){ System.out.println ( "no opinion" ) ; } if ( x == 3 ){ System.out.println ( "agree" ) ; } }

Write the definition of a method printDottedLine, which has no parameters and doesn't return anything. The method prints to standard output a single line (terminated by a newline) consisting of five periods.

public static void printDottedLine (){ System.out.println("....."); }

Write the definition of a method printGrade, which has a char parameter and returns nothing. The method prints on a line by itself the message string Grade: followed by the char parameter (printed as a character) to standard output. Don't forget to put a new line character at the end of your line.

public static void printGrade(char x){ System.out.println("Grade: "+x); }

Write the definition of a method printLarger, which has two int parameters and returns nothing. The method prints the larger value of the two parameters to standard output on a single line by itself.

public static void printLarger (int x,int y){ int z = Math.max(x,y); System.out.println(z); }

Write the definition of a method dashedLine, with one parameter, an int. If the parameter is negative or zero, the method does nothing. Otherwise it prints a complete line terminated by a newline to standard output consisting of dashes (hyphens) with the parameter's value determining the number of dashes. The method returns nothing.

public void dashedLine (int a){ if (a>0){ int i; for (i=1;i<=a;i=i+1){ System.out.print("-"); } System.out.println(""); } }

Write the code for invoking a method named sendObject. There is one argument for this method which is of type Customer. Assume that there is a reference to an object of type Customer, in a variable called John_Doe. Use this reference as your argument. Assume that sendObject is defined in the same class that calls it.

sendObject(John_Doe);

Write the code for invoking a method named sendTwo. There are two arguments for this method: a double and an int. Invoke the method with the double value of 15.955 and the int value of 133. Assume that sendTwo is defined in the same class that calls it.

sendTwo(15.955,133) ;

Write the code for invoking a method named sendVariable. There is one int argument for this method. Assume that an int variable called x has already been declared and initialized to some value. Use this variable's value as an argument in your method invocation. Assume that sendVariable is defined in the same class that calls it.

sendVariable(x) ;

Write the definition of a method add, which receives two integer parameters and returns their sum.

public static int add(int a, int b) { int c = a + b; return c; }

Assume that x is a variable that has been declared as an int and been given a value. Assume that twice is a method (in the same class as the caller) that receives a single integer parameter and returns twice its value. (So if you pass 7 to twice it will return 14. Thus the expression twice(7) has the value 14. Write an expression whose value is eight times that of x without using the standard Java arithmetic operators (*,+, etc.). Instead, use calls to twice to accomplish this. In this exercise you must write this as a single expression-- you must not write any statements. Also, you may only use the twice() function-- no other functions or operators.

twice(twice(twice(x)))

add is a method that accepts two int arguments and returns their sum. Two int variables, euroSales and asiaSales, have already been declared and initialized. Another int variable, eurasiaSales, has already been declared. Write a statement that calls add to compute the sum of euroSales and asiaSales and that stores this value in eurasiaSales. Assume that add is defined in the same class that calls it.

eurasiaSales = add ( euroSales, asiaSales);

max is a method that accepts two int arguments and returns the value of the larger one. Four int variables, population1, population2, population3, and population4 have already been declared and initialized. Write an expression (not a statement!) whose value is the largest of population1, population2, population3, and population4 by calling max. Assume that max is defined in the same class that calls it.

max(max(population1, population2), max(population3, population4))

max is a method that accepts two int arguments and returns the value of the larger one. Two int variables, population1 and population2, have already been declared and initialized. Write an expression (not a statement!) whose value is the larger of population1 and population2 by calling max. Assume that max is defined in the same class that calls it.

max(population1,population2)

printLarger is a method that accepts two int arguments and returns no value. Two int variables, sales1 and sales2, have already been declared and initialized. Write a statement that calls printLarger, passing it sales1 and sales2. Assume that printLarger is defined in the same class that calls it.

printLarger ( sales1 , sales2 );

printTodaysDate is a method that accepts no arguments and returns no value. Write a statement that calls printTodaysDate. Assume that printTodaysDate is defined in the same class that calls it.

printTodaysDate();

Write a method max that has two string parameters and returns the larger of the two.

public String max (String aString, String bString) { if (aString.compareTo(bString) > 0) { return aString; } else { return bString; } }

Write the code for invoking a method named sendNumber . There is one int argument for this method. Send the number 5 as an argument to this method. Assume that sendNumber is defined in the same class that calls it.

sendNumber(5) ;

Given that a method receives three parameters a, b, c, of type double, write some code, to be included as part of the method, that determines whether the value of "b squared" - 4ac is negative. If negative, the code prints out the message "no real solutions" and returns from the method

if((Math.pow(b,2) - 4 * a * c) < 0) { System.out.print("no real solutions"); }


Ensembles d'études connexes

CH 5: The Self-Employment Contributions ACT (SECA)

View Set

Chapter 24 Homework: The Digestive System

View Set

Chapter 1 Fundamentals of Nursing

View Set

Chapter 17 LearnSmart Marketing 021 Ybarra

View Set

Chapter 45: Renal and Urologic Problems

View Set

PADM 3100 - Workplace Conflict (Quizzes/Test for Entire Course)

View Set