You are tasked with implementing the functionality of a Student class which calculates…
- User
You are tasked with implementing the functionality of a Student class which calculates and assigns the student's grade based on the provided marks.
A student's grade is determined by the following criteria:
If the student's marks are less than 0 or greater than 100, the grade should be 'X'.
If the student's marks are greater than or equal to 90, the grade should be 'A'.
If the student's marks are greater than or equal to 80 but less than 90, the grade should be 'B'.
If the student's marks are greater than or equal to 70 but less than 80, the grade should be 'C'.
If the student's marks are greater than or equal to 60 but less than 70, the grade should be 'D'.
If the student's marks are greater than or equal to 50 but less than 60, the grade should be 'E'.
If the student's marks are less than 50, the grade should be 'F'.
Class Structure
The class Student contains the following:
An instance variable marks of type int.
A constructor that accepts an int parameter to initialize marks.
A method assignGrade() that returns a char, representing the student's grade based on the provided marks.
Here's the skeleton of the class you will work with:
public class Student { private int marks;
public Student(int marks) { // TODO: Assign 'marks' to the instance variable 'this.marks' } public char assignGrade() { // TODO: Implement the method which assigns the grade based on marks. // If marks are less than 0 or greater than 100, return 'X' // If marks are greater than or equal to 90, return 'A' // If marks are greater than or equal to 80, return 'B' // If marks are greater than or equal to 70, return 'C' // If marks are greater than or equal to 60, return 'D' // If marks are greater than or equal to 50, return 'E' // If marks are less than 50, return 'F' }} Task
Your task is to implement the Student(int marks) constructor and assignGrade() method in the Student class.
Student(int marks) - This constructor will take one parameter, marks (an integer), and it should assign it to the marks instance variable.
assignGrade() - This method should return a character representing the grade of the student, based on the marks they have received.
If marks are:
less than 0 or greater than 100, return 'X'
greater than or equal to 90, return 'A'
greater than or equal to 80, return 'B'
greater than or equal to 70, return 'C'
greater than or equal to 60, return 'D'
greater than or equal to 50, return 'E'
less than 50, return 'F'
Examples
Here are some examples of how your Student class should behave once implemented:
- Assistant
public class Student { private int marks;
public Student(int marks) { this.marks = marks; } public char assignGrade() { if (marks < 0 || marks > 100) { return 'X'; } else if (marks >= 90) { return 'A'; } else if (marks >= 80) { return 'B'; } else if (marks >= 70) { return 'C'; } else if (marks >= 60) { return 'D'; } else if (marks >= 50) { return 'E'; } else { return 'F'; } }}
// Example usage Student student1 = new Student(85); System.out.println(student1.assignGrade()); // Should print 'B'
Student student2 = new Student(101); System.out.println(student2.assignGrade()); // Should print 'X'
- User
Coding Exercise: Weather Advisor In this problem, you have to complete the implementation of a WeatherAdviser class in Java that provides advice on what to wear based on the current temperature.
Here is the skeleton of the WeatherAdviser class:
public class WeatherAdviser {
public String provideWeatherAdvisory(int temperature) { // TODO: Provide a weather advisory message based on the temperature. return ""; }} Task
Your task is to implement the provideWeatherAdvisory(int temperature) method in the WeatherAdviser class.
provideWeatherAdvisory(int temperature) - This method will take one parameter, temperature (an integer), and it should return a String containing advice on what to wear according to the following guidelines:
If the temperature is less than 0, return "It's freezing! Wear a heavy coat."
If the temperature is between 0 and 10, inclusive, return "It's cold! Bundle up."
If the temperature is between 11 and 20, inclusive, return "It's cool! A light jacket will do."
- Assistant
public class WeatherAdviser {
public String provideWeatherAdvisory(int temperature) { if (temperature < 0) { return "It's freezing! Wear a heavy coat."; } else if (temperature >= 0 && temperature <= 10) { return "It's cold! Bundle up."; } else if (temperature >= 11 && temperature <= 20) { return "It's cool! A light jacket will do."; } else { return "It's warm! Enjoy the day."; } }}
// Example usage WeatherAdviser adviser = new WeatherAdviser();
System.out.println(adviser.provideWeatherAdvisory(-5)); // Output: "It's freezing! Wear a heavy coat."
System.out.println(adviser.provideWeatherAdvisory(5)); // Output: "It's cold! Bundle up."
System.out.println(adviser.provideWeatherAdvisory(15)); // Output: "It's cool! A light jacket will do."
System.out.println(adviser.provideWeatherAdvisory(25)); // Output: "It's warm! Enjoy the day."
- User
Coding Exercise: Switch with Char - Is a Vowel or Not You are tasked with creating a method in the MyChar class to determine if a given character is a vowel.
Here's a skeleton of the MyChar class for reference:
public class MyChar {
// Method to determine if a character is a vowel public boolean isVowel(char ch) { // Use a switch statement to check for each vowel, both lowercase and uppercase switch (ch) { // TODO: Complete the switch statement } return false; }} Task
Your task is to implement the switch statement inside the isVowel(char ch) method.
The switch statement should return true if the character is a vowel (either lowercase or uppercase) and false otherwise. The vowels are: 'A', 'E', 'I', 'O', 'U', and their respective lowercase versions.
Examples
Here are some examples of how your completed MyChar class should behave:
MyChar myChar = new MyChar();
System.out.println(myChar.isVowel('A')); // Output: true System.out.println(myChar.isVowel('b')); // Output: false System.out.println(myChar.isVowel('E')); // Output: true System.out.println(myChar.isVowel('f')); // Output: false Good luck with your implementation!
- Assistant
public class MyChar {
// Method to determine if a character is a vowel public boolean isVowel(char ch) { switch (Character.toUpperCase(ch)) { case 'A': case 'E': case 'I': case 'O': case 'U': return true; default: return false; } }}
// Example usage MyChar myChar = new MyChar();
System.out.println(myChar.isVowel('A')); // Output: true System.out.println(myChar.isVowel('b')); // Output: false System.out.println(myChar.isVowel('E')); // Output: true System.out.println(myChar.isVowel('f')); // Output: false