Java(class one review problem solving)
1. Open the Dog.java file and create a Dog class. Include the following private class variables: name breed weight All class variables should be declared lowercase. In other words, you should follow naming and coding conventions. The class should include two constructors: The main constructor that initializes all the class variables to the given arguments A secondary constructor that takes in only Name and Weight (Breed defaults to "Mutt") The class should include getter methods for all class variables. 2. You can test your code in the Main.java class by creating a Dog object using both Constructors and printing their fields/states to make sure it's correct. Main.java class Main { public static void main(String[] args) { } } Dog.java file
Main.java class Main { public static void main(String[] args) { } } Dog.java public class Dog{ private String name; private String breed; private double weight; public Dog(String name, String breed, double weight){ this.name = name; this.breed = breed; this.weight = weight; } public Dog(String name, double weight){ this.name = name; this.breed = "Mutt"; this.weight = weight; } public String getName(){ return name; } public void setName(String name){ this.name = name; } public String getBreed(){ return breed; } public void setBreed(String breed){ this.breed = breed; } public double getWeight(){ return weight; } public void setWeight(double weight){ this.weight = weight; } }
Complete the maxLengthWord method so that given the array words, it will return the word with the largest length. class Main { public static void main(String[] args) { String[] arr = {"hey","yolo","hi","this is long"}; System.out.println(maxLength(arr)); //should print "this is long" } public static String maxLength(String[] names) { //write code here! } }
class Main { public static void main(String[] args) { String[] arr = {"hey","yolo","hi","this is long"}; System.out.println(maxLength(arr)); //should print "this is long" } public static String maxLength(String[] names) { int size = names.length; String longestName = names[0]; for(int i = 1; i < size; i++){ if(names[i].length() > longestName.length()){ longestName = names[i]; } } return longestName; } }
Complete the avgElements method so that it will average all the elements in an integer array (passed to the method as a parameter) and return the average. class Main { public static void main(String[] args) { int[] arr = {2,7,3,8,4}; System.out.println(avgElements(arr)); //should print 4.8 }//end method //write code here! public static double avgElements(int[] array) { }//end method }//end class
class Main { public static void main(String[] args) { int[] arr = {2,7,3,8,4}; System.out.println(avgElements(arr)); //should print 4.8 }//end method public static double avgElements(int[] array) { //write code here! int sum = 0; for(int i=0; i<array.length; i++){ sum+=array[i]; } return (double) sum / array.length; }//end method }//end class
Write a method header on line two with the following specs: Returns: 1. an integer Name: 2. sumEvenToX Parameters 3. an integer called "x" Purpose: calculate the sum of the EVEN integers from 1 to x (including x) class Main { //Add method header { // Add logic to achieve method purpose } } public static void main(String[] args){ Main mainObj = new Main(); System.out.println(mainObj.sumEvenToX(5)); //6 System.out.println(mainObj.sumEvenToX(8)); //20 }
class Main { //Add method header public static int sumEvenToX(int x) { // Add logic to achieve method purpose int total = 0; for(int i= 1; i<=x; i++){ if(i%2==0){ total += i; } } return total; } //test case below (dont change): public static void main(String[] args){ Main mainObj = new Main(); System.out.println(mainObj.sumEvenToX(5)); //6 System.out.println(mainObj.sumEvenToX(8)); //20 } }
Creating Variables and Printing 5 For you to do: Create a String variable called "name" and set it to "Chen" Create an integer variable called "age" and set it to 50 Create an integer variable called "iq" and set it to the value of age (do NOT use ' = 50') Print the value of name Print the value of age without skipping a new line Print the value of iq
class Main { public static void main(String[] args) { //write code here! String name = "Chen"; int age = 50; int iq = age; System.out.println(name); System.out.print(age); System.out.println(iq); } }
For you to do: Create a String named "name" and assign the value "Timmy" to it Print out the length of the string variable 'name'
class Main { public static void main(String[] args) { //write code here! String name = "Timmy"; System.out.println(name.length()); } }
For you to do: Given a string variable "word", do the following tests If the word ends in "y", print "-ies" If the word ends in "ey", print "-eys" If the word ends in "ife", print "-ives" If none of the above is true, print "-s" No more than one should be printed. import java.util.Scanner; class Main { public static void main(String[] args) { Scanner inp = new Scanner(System.in); System.out.print("In: "); String word = inp.nextLine(); } }
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner inp = new Scanner(System.in); System.out.print("In: "); String word = inp.nextLine(); //DO NOT CHANGE ABOVE CODE! Write your code below if(word.endsWith("y")){ System.out.println("-ies"); } else if(word.endsWith("ey")){ System.out.println("-eys"); } else if(word.endsWith("ife")){ System.out.println("-ives"); } else{ System.out.println("-s"); } } }
DUE DATE No due date Unlimited Submissions Write a for loop that will print out the numbers starting at 1 and ending at the input inclusive. The numbers printed should all be on the same line separated by a space. import java.util.Scanner; class Main { public static void main(String[] args) { Scanner inp = new Scanner(System.in); System.out.print("In: "); int end = inp.nextInt(); } }
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner inp = new Scanner(System.in); System.out.print("In: "); int end = inp.nextInt(); //write your code below for(int i = 1; i <= end; i++){ System.out.print(i + " "); } } }
The variable "num" holds an integer user input Write a conditional statement starting on line 9 that does the following: If num is positive, print "__ is positive" If num is negative, print "__ is negative" import java.util.Scanner; class Main { public static void main(String[] args) { Scanner inp = new Scanner(System.in); System.out.print("In:"); int num = inp.nextInt(); } }
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner inp = new Scanner(System.in); System.out.print("In:"); int num = inp.nextInt(); //DO NOT CHANGE ABOVE CODE! Write your code below if(num > 0 ){ System.out.println(num + " is positive"); } else if(num < 0){ System.out.println(num + " is negative"); } } }
Using a for loop, determine whether the integer variable max is prime or not. If the number is prime, print out "prime". If it's not, print out "not prime". Keep in mind that 1 is NOT a prime number and a prime number is not divisible by any other number except itself and 1. The program should expect only values that are 1 or greater (i.e., you do not need to worry about 0 or negative values. import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("In: "); int value = input.nextInt(); } }
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("In: "); int value = input.nextInt(); //write your code below boolean isPrime = false; for(int i = 2; i<= value/2; i++){ if(value % i == 0){ isPrime = true; break; } } if(!isPrime){ System.out.println("prime"); } else { System.out.println("not prime"); } } }