Java short answer 1)What does the static keyword mean? 2)Why and how do you use
ID: 3581222 • Letter: J
Question
Java
short answer
1)What does the static keyword mean?
2)Why and how do you use a try/catch block?
3)What does the void keyword mean?
code Evaluation
What is the error in the code?
Boolean good = true;
while(good =true) {
//do stuff
}
Integer Methods
blackjack: Given a pair of ints, return the int that is closest to 21 without going over.
// blackjack(7,17) -> 17
// blackjack(21,16) -> 21
// blackjack(19,23) -> 19
public intblackjack(int a, int b) {
}
isDivisible: Returns true if x is divisible by y
.
// isDivisible(2,100) -> false
// isDivisible(4,2) -> true
// isDivisible(123,3) -> true
public Boolean isDivisible(int x, int y) {
}
String Methods
unOrUn: If the String begins with un", return a String with without the un" in front. Otherwise, return the String with un" added to the front of it. You may assume the String is at least 3 characters long.
// unOrUn("untied") -> "tied"
// unOrUn("unable") -> "able"
// unOrUn("necessary") -> "unnecessary"
Public String unOrUn(String str) {
}
hasWildcat: Given an input String str, return true if str contains the String cat" in it, but the middle `a' can be any char.
// hasWildcat("kitty") -> false
// hasWildcat("tomcat") -> true
// hasWildcat("c4tn1P") -> true
public Boolean hasWildcat(String str) {
}
// A-- would not buy again
Array Methods
maxMinDiff: Given an array of ints, return the difference between the maximum element and the minimum element. You can assume your array will have 2 or more elements in it.
// [1,2,3,4,5] -> 4
// [15,31,21,17,28] -> 16
// [-1,-100,12,2,100] -> 200
public int maxMinDiff(int[] arr) {
}
swapEnds: Given an array of ints, return the array with the first and last elements swapped. You can assume your array will have 2 or more elements in it.
// [1,2,3,4,5] -> [5,2,3,4,1]
// [15,31,21,17,28] -> [28,31,21,17,15]
// [-1,-100,12,2,100] -> [100,-100,12,2,-1]
public int swapEnds(int[] arr) {
}
Project Euler Problems
euler1: If we list all the multiples of 3 or 5 that are <10, we get 3, 5, 6 and 9. The sum of these multiples is 23. euler1 returns the sum of all multiples of 3 or 5 below int limit.
public int euler1(int limit) {
}
euler2: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1;2;3;5;8;13;21;34;55;89; :::
By considering the terms in the Fibonacci sequence whose values do not exceed four million, write a program to find the sum of the even-valued terms.
public inteuler2() {
}
Explanation / Answer
Static Keyword : Basically, static keyword deals with memory management. In Java, this keyword can be used with method, variable, initialization block and class nested within another class. And this keyword cannot be used with constructor, interface, method local inner class, inner class methods, instance variable, local variable and non nested class.
The static keyword can be used to attach a Variable or Method to a Class. The variable or Method that are marked static belong to the Class rather than to any particular instance. Static variable can be used without creating any instance or object of the class only class name is required to invoke the static method or variable. That’s why we are writing static keyword in main method so that we don’t require creating object to invoke main.
Try Catch : An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally. Whenever exception occurs we need to handle the exception so that program can run normally. Basically, in try block we write the code in which exception may occur. Every try block is followed by either catch block or finally block. Catch block is used to handle the exception. We can use multiple catch block with single try block.
Syntax:
try{
//code that may throw exception
}catch(Exception_class_Name ref){
} catch(Exception e){
}
Void : void means null. In java, suppose a method does not return any value so we need to define it as void means that this method will not return anything. In main method of java generally we don’t written anything that’s why we write its written type as void.
Boolean good = true;
while(good =true) {
//do stuff
}
Here in while, we are doing comparison but we are writing assignment operator i.e. = instead of ==. Anyhow this block will run infinite time as we assigning true to good variable in the while block also.
public int blackjack(int a, int b) {
if(Math.abs(21-a)<= Math.abs(21-b)){
return a;
}
else{
return b;
}
}
public Boolean isDivisible(int x, int y) {
if(x%y == 0){
return true;
}
else{
return false;
}
}
public String unOrUn(String str) {
if(str.startsWith("un")){
return str.substring(2, str.length());
}else{
return "un"+str;
}
}
public Boolean hasWildcat(String str) {
for(int i = 0; i < str.length()-2 ;i++){
if(str.charAt(i) == 'c' && str.charAt(i+2)=='t'){
return true;
}
}
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.