Question

In: Computer Science

Write the basic JAVA code for the beginners and follow all the instructions listed 1. A...

Write the basic JAVA code for the beginners and follow all the instructions listed

1. A year with 366 days is called a leap year. A year is a leap year if it is divisible by 4 (for e.g., 1980), except that it is not a leap year if it is also divisible by 100 (for e.g., 1900); however, it is a leap year if it is further divisible by 400 (for e.g., 2000).

Write a program that prompts the user to enter a year and displays whether that year is a leap year. The program should repeatedly prompt the user for a year until the user enters a sentinel value of -99 to quit the program. Note that you will need to use the mod function (%) to determine divisibility by a number. You CANNOT use the class GregorianCalendar and any of its methods.

2. Write a java program to print student’s total marks, percentage and grade. User input is given to enter number of subjects and then prompts user to enter marks. Calculate Total marks, Student Percentage, Student Grade.

Note:

1. Maximum marks for a subject is 100.

2. percentage = (totalMarks/(count*100)) * 100;

>=90 is Grade A

>=80 and <=89.99 is Grade B

>=70 and <=79.99 is Grade C

>=60 and <=69.99 is Grade D

< 60 is Fail

Tips: use scanner for inputs, use for loop (to iterate and store marks), use switch statement (to print grades).

Sample Output:

Enter Number of Subject(s): 3

Enter Marks of 3 Subject(s):

70

80

90

Total Marks : 240

Student Percentage: 80

Student Grade : B

Solutions

Expert Solution

Program 1:-

Screenshot of program code:-

Screenshot of output:-

Program code to copy:-

//Program to check whether a given year is leap year or not
import java.util.Scanner;
public class LeapYear {

   public static void main(String[] args) {
       Scanner stdin = new Scanner(System.in); // For standard input
       int year;
       do {
           //Prompt & read a year from user
         System.out.print("Enter a year <enter sentinel value -99 to quit the program>: ");
         year = stdin.nextInt();
        
         if(year !=-99) {
         //Checking if a year is divisible by 4
             if(year % 4 == 0) {
                 //Checking if a year is divisible by 100
                 if(year % 100 == 0) {
                 //Checking if a year is divisible by 400
                     if(year % 400 == 0)
                         System.out.println(year + " is a Leap Year");
                     else
                         System.out.println(year + " is not a Leap Year");
                 }
                 else
                     System.out.println(year + " is a Leap Year");
             }
             else
                 System.out.println(year + " is not a Leap Year");
         }
        
       }while(year!=-99);
      
   }
}

Program 2:-

Screenshot of program code:-

Screenshot of output:-

Program code to copy:-

import java.util.Scanner;
public class StudentMarksGrade {

   public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
      
       //Prompt & read Number of Subject(s) from user
       System.out.print("Enter Number of Subject(s): ");
int count = in.nextInt();
  
double totalMarks = 0;
//Prompt the user to enter marks of subjects
System.out.println("Enter Marks of " + count + " Subject(s): ");
for(int i=1; i<=count; i++) {
   //Read the marks of subjecy from user
double marks = in.nextDouble();
//Calculate total marks
totalMarks += marks;
}
//Calculate percentage
double percentage = (totalMarks/(count*100)) * 100;
  
//Display total marks & percentage
System.out.println("Total Marks: " + totalMarks);
System.out.println("Student Percentage: " + percentage);
  
//Determine grade
char grade;
if(percentage>=90)
   grade = 'A';
else
if(percentage>=80)
   grade = 'B';
else
if(percentage>=70)
   grade = 'C';
else
if(percentage>=60)
   grade = 'D';
else
   grade = 'F';
  
//Display grade
switch(grade) {
   case 'A': System.out.println("Student Grade : " + grade);
           break;
   case 'B': System.out.println("Student Grade : " + grade);
                   break;
   case 'C': System.out.println("Student Grade : " + grade);
                   break;
   case 'D': System.out.println("Student Grade : " + grade);
                   break;
   case 'F': System.out.println("Student Grade : Fail");
}
  
   }

}


Related Solutions

JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString()...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString()...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads three strings from the keyboard. Although the strings are in no particular order, display the string that would be second if they were arranged lexicographically.
I am in beginners java course so using the most simple/basic JAVA please complete the following...
I am in beginners java course so using the most simple/basic JAVA please complete the following CODE SEGMENTS. (2 parts) FIRST PART: Using ITERATIVE create a METHOD MAX that returns the max element in an ArrayList of ints and prints all of the elements. *you have to use an iterative in the method.* (just need to write the METHOD ONLY not a whole program(will do in 2nd part), assume you are given any numbers/integers. SECOND PART: Now write a class...
I need code written in java for one of my projects the instructions are Write a...
I need code written in java for one of my projects the instructions are Write a program that interacts with the user via the console and lets them choose options from a food menu by using the associated item number. It is expected that your program builds an <orderString> representing the food order to be displayed at the end. (See Sample Run Below). Please note: Each student is required to develop their own custom menus, with unique food categories, items...
The code is bellow. follow this instructions to edit the code : lets assume that instead...
The code is bellow. follow this instructions to edit the code : lets assume that instead of the maximum and minimum values, you want to find the 2nd largest/smallest. For example, in the numbers 1 to 10, this would return 2 and 9 .you will have to change the "max_min" function accordingly. this has a different puepose than "max_min", so you must give it another name. code: #include <stdio.h> #define N 235 void max_min (int a[], int n, int *max,...
As code is given below, follow the instructions: 1. Count the number of comparisons and find...
As code is given below, follow the instructions: 1. Count the number of comparisons and find where to put that counter in the code 2. Pick a random pivot, right pivot, left pivot, middle pivot for each smaller array /sub-array import java.util.*; import java.util.List; public class QuickSort { public static void main(String[] args) { int[] values = { 6, 5, 4, 3, 1, 7, 8 }; System.out.println("Original order: "); for (int element : values) System.out.print(element + " "); IntQuickSorter.quickSort(values); System.out.println("\nFirst...
This code has to be in java (I code in eclipse). Also these instructions have to...
This code has to be in java (I code in eclipse). Also these instructions have to be followed exactly because if not my output won't match the expected out ( this will be uploaded on zybooks). Write a program that asks the user for their age in days. The program will compute the person's age in years (you can assume that all years have 365 days) and then prints one of the following messages: If the user is 1 year...
Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using...
Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using recursion, NOT LOOPS. You may use JDK String Methods like substring() and length(), but do not use the JDK methods to avoid coding algorithms assigned. For example, don’t use String.revers(). public boolean recursiveContains(char c, String s) { if (s.length() == 0) return false; if (s.charAt(s.length() - 1) == c) return true; else return recursiveContains(c, s.substring(0, s.length() - 1)); } public boolean recursiveAllCharactersSame(String s) return...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements. Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT