Question

Need help with a few questions! Using the JAVA language please. 1) exercises - branching Write...

Need help with a few questions! Using the JAVA language please.

1) exercises - branching

Write a method whatToWear(int temp) that takes a temperature and then outputs a string for what to wear in different weather conditions. There must be at least 3 categories. For example, whatToWear(90) might return “shorts” and whatToWear(30) might return “down coat”

2) Enum exercise

• Define an enum Seasons, and rewrite the whatToWear method to use enums and switch statement

8) 2D array exercise

1. write a method columnSum(int[][] a, int col) that returns the sum of the values of column col

2. Write a method public static boolean isSquare(int[][] a) that checks if the array is square (i.e. every row has the same length as a itself)

9) Classes Exercise

• Consider a class Student

• Think of four instance variables

• Write four possible constructors

10) Classes exercise

• Define a class BlogEntry that has variables

• username

• text

• date

Methods

• getters/setters

• displayEntry()

• getSummary() // return first 10 words

11) Ragged array exercise

Create a ragged array with contents:

0

1 2

3 4 5

6 7 8 9

10 11 12 13 14

Thanks in advance.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note : Sudden power cut..i will update remaining...thank u

____________________________

1)

// BasedOnTemp.java

public class BasedOnTemp {
   public static void main(String args[]) {
       int temp = 40;
       String type = whatToWear(temp);
       System.out.println(type);
   }

   private static String whatToWear(int temp) {
       String str = "";
       if (temp >= 0 && temp <= 35) {
           str = "down coat";
       } else if (temp > 35 && temp <= 50) {
           str = "casuals";
       } else if (temp > 50) {
           str = "shorts";
       }
       return str;

   }
}
___________________________

Output:

casuals
___________________________

8)

1)

// TwoDArray.java

public class TwoDArray {

   public static void main(String[] args) {
       int arr[][]={{1,2,3},{3,4,5},{4,5,6}};
       for(int i=0;i<arr[0].length;i++)
       {
       System.out.println("Sum of Elements in Column#"+(i+1)+":"+columnSum(arr, i));
       }

   }
   public static int columnSum(int[][] a, int col)
   {
       int sum=0;
       for(int i=0;i<a.length;i++)
       {
           sum+=a[i][col];
       }
       return sum;
   }
}
___________________________

Output:

Sum of Elements in Column#1:8
Sum of Elements in Column#2:11
Sum of Elements in Column#3:14

__________________________

2)

package org.students;

public class CheckSquare {

   public static void main(String[] args) {
       int arr1[][]={{3,4},{4,5,7},{6,7,8,9}};
      
boolean b=isSquare(arr1);  
if(b)
{
   System.out.println("The 2-D array is square");
}
else
{
   System.out.println("The 2-D array is not square");
}
int arr2[][]={{3,4,5},{4,5,7},{6,7,8}};
       
b=isSquare(arr2);  
if(b)
{
   System.out.println("The 2-D array is square");
}
else
{
   System.out.println("The 2-D array is not square");
}
  

   }
   public static boolean isSquare(int[][] a)
   {
       for(int i=0;i<a.length;i++)
       {
           if(a.length!=a[i].length)
           {
               return false;
           }
       }
       return true;
   }

}
_______________________________

Output:

The 2-D array is not square
The 2-D array is square

________________________________

Add a comment
Know the answer?
Add Answer to:
Need help with a few questions! Using the JAVA language please. 1) exercises - branching Write...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with a few programming exercises. The language being used for all of them is...

    Need help with a few programming exercises. The language being used for all of them is JAVA. 1) exercises - branching (language is JAVA) • Write a method whatToWear(int temp) that takes a temperature and then outputs a string for what to wear in different weather conditions. There must be at least 3 categories. For example, whatToWear(90) might return “shorts” and whatToWear(30) might return “down coat” 2) Enum exercise • Define an enum Seasons, and rewrite the whatToWear method to...

  • Need help to create general class Classes Data Element - Ticket Create an abstract class called...

    Need help to create general class Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...

  • java with Part 1 Write the Java classes given with their fields. Implement their constructors, toString...

    java with Part 1 Write the Java classes given with their fields. Implement their constructors, toString methods, getters and setters. Then create three Ticket instances. • Airport: String name, String city, String country • Route: Airport departure , Airport destination • Date: int day, int month, int year (Day must be between 0-31, month must be between 0-12, year must be between 2020-2025; please consider this when you're implementing setters and constructors). • ClockTime: int hour, int minute (Hour must...

  • Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman...

    Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman, sophomore, junior, or senior). Define the possible status values using an enum. Staff has an office, salaray, and date-hired. Implement the above classes in Java. Provide Constructors for classes to initialize private variables. Getters and setters should only be provided if needed. Override the toString() method...

  • Write code in Java programming language. The ShoppingCart class will be composed with an array of...

    Write code in Java programming language. The ShoppingCart class will be composed with an array of Item objects. You do not need to implement the Item class for this question, only use it. Item has a getPrice() method that returns a float and a one-argument constructor that takes a float that specifies the price. The ShoppingCart class should have: Constructors: A no-argument and a single argument that takes an array of Items. Fields: • Items: an array of Item objects...

  • (JAVA) 1) Write a blueprint class that tracks the orders made to vendors in a company's...

    (JAVA) 1) Write a blueprint class that tracks the orders made to vendors in a company's product purchasing system. We will create a class called ProductOrder. The requirements are as follows: a) The class ProductOrder has the following private instance variables:productID orderld, quantityRequested quantity Fulfilled all of types integer. We will need separate variables for quantity requested and quantity fulfilled because the vendors may not be able to fulfill the requests completely b) The class/object model will provide the values...

  • Hi, the language is Java. Learning Outcomes and Introduction In this lab assignment you will practice:...

    Hi, the language is Java. Learning Outcomes and Introduction In this lab assignment you will practice: . applying access modifiers using getters and setters to mediate access using getters and setters to create derived attributes using static variables and methods testing code using a unit testing approach . For each of the tasks in this lab, you will create a new class or create an improved version of one of the classes from the previous lab. Remember to document all...

  • please answer 1-5 Section 4. Write the following programs. (34 points) 1. (7 points) Define a...

    please answer 1-5 Section 4. Write the following programs. (34 points) 1. (7 points) Define a class called MyLinkedQueue containing two data field: head and tail. Create constructors, setters and getters as appropriate. 2. (7 points) Define an inner class called Queue Node inside MyLinkedQueue containing necessary data fields, and constructors, to make sure the queue can move back and forth. 3. (10 points) Implement the poll and offer methods for the MyLinkedQueue class. 4. (5 points) Implement the isPalindrome...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • Using Java write a program that performs the following: 1. Define a class that contains an...

    Using Java write a program that performs the following: 1. Define a class that contains an array of type int as a data member. 2. Define a constructor that creates an array of 1024 elements and assigns it to the class data member. The constructor shall populate the elements with random numbers ranging in value from 1 to 1024. 3. Define and implement a search() method that take a parameter of type int and returns the index location if the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT