• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2024
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Java Assignment for Control Flow and Class-Object | Students Mark Range Check

Pranati Paidipati/9234/0
CodeJAVA

This is Java programming assignment to test your control flow and object-oriented concepts in Java.

PROBLEM STATEMENT:

Write a program in java called Marks (Marks.java) which prompts the user for the number of marks, reads it from the keyboard, and saves it in an integer variable called numOfMarks. It then prompts the user for the marks and saves them in an integer array called marks.

The program you code shall check that value of marks entered are in the range 0 to 100 and the number of marks entered between 0 to 30. The program should display the number of marks that are more than 50.

An example of the output is shown below:

Enter the number of marks: 2

Enter marks 1 : 145

Invalid marks.. try again !

Enter marks 1: 45

Enter marks 2 : 56

Number of marks more than 50 is: 1

Prerequisite for Solving This Java Assignment:

  • Control Flow Statements in Java
  • Class and Object in Java with Real Time Example

Program: Java Assignment for Control Flow and Class-Object

package Marks;
import java.util.Scanner;

public class Marks {   
  int numOfMarks;
  int[] marks;
  int count = 0;

  Scanner input;

  /*
  * function that checks if input is valid or not 
  * and also prints the number
  * of marks > 50
  */

  private void checkMarks(int numOfMarks) {
    int i = 0;
    int score = 0;

    //to check if number of marks are in 
    //between 0 and 30

    if (numOfMarks <= 0 || numOfMarks >= 30) {
      System.out.println("Invalid Input !");
      return;
    }

    marks = new int[numOfMarks];

    while (i < numOfMarks) {
      System.out.printf("Enter marks %d:", i + 1);
      score = input.nextInt();

      // to check if value of marks is in between 0 to 100

      if (score >= 0 && score <= 100) {
        marks[i] = score;   
        // assigning value of score variable to marks array
        i++;
        continue;
      }
      System.out.println("Invalid marks.. Try again !!");
    }

    // loop the marks entered to check 
    // if marks obtained are greater than 50

    for (int j = 0; j < marks.length; j++) {
      if (marks[j] >= 50) {
        count++;
      }
    }
    System.out.println("Number of marks greater than 50:" + count);
  }
  public static void main(String args[]) {
    Marks myMarks = new Marks();
    myMarks.input = new Scanner(System.in);
    System.out.println("Enter the number of marks:");
    myMarks.numOfMarks = myMarks.input.nextInt();                             
    myMarks.checkMarks(myMarks.numOfMarks);
  }
}

Code Explanation:

Now, let’s have a look at what’s actually happening in the program coded above.

  • Here as directed in the problem statement we have taken a variable numOfMarks of int type and an integer array called marks.
  • The use of the Scanner class helps us by taking input from the user.
  • A variable count of integer type with an initial value 0 has been initialized.
  • In the above program, we have used a function checkmarks() to check if the variable numOfMarks is in between 0 to 30 and the value of marks entered in the array is in between 0 to 100.

Floww and Working of Code:

  • Coming to the heart of the program, i.e., the main function, where we have first created an object called myMarks of the class Marks.
  • Then later prompted the user to enter the number of marks he wishes to add.
  • After taking user input, the checkmarks(int numOfMarks) is called to execute the core functionality of the program.
  • The core functionality of the program here refers to checking if the user input is valid or not and also updating the count of the number of marks that are more than 50.
  • The value of the count variable is incremented every time a value greater than 50 is encountered in the marks array.

Java Program Output:

Now, let’s have a look at the various possible outputs for the above program.

Case 1: If the user inputs a value less than 0 or greater than 30 for the variable numOfMarks

Enter the number of marks: 0
Invalid Input !

Case 2: If the user inputs mark greater than 100 and less than 0, we get-

Enter the number of marks: 2
Enter marks 1: 154
Invalid marks.. Try again !
Enter marks 1: 56
Enter marks 2: 47
Number of marks greater than 50: 1

Case 3: Expected Output

Enter the number of marks: 3
Enter marks 1: 56
Enter marks 2: 65
Enter marks 3: 47
Number of marks greater than 50: 2

There might be other ways to solve this Java Assignment for Control Flow and Class-Object. You can try them out and even share it with us.

If you like solving coding challenges, checkout out these coding questions asked in many of the interview rounds.

Keep Coding and Happy Learning!

Javajava assignment
Pranati Paidipati
I am a graduate in computer science with a creative bent of mind for writing content. With great gusto, I enjoy learning new things. I trail in database management system, and object-oriented programming languages like Java, C/C++.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Basic Java Tutorial

  1. Java- Tutorial Overview
  2. Java- Features & Characteristics
  3. Java-  Installation & Setup
  4. Java- Hello, World Program!
  5. Java- JDK vs JVM vs JRE
  6. Java- Data Types & Variables
  7. Java- String & its Methods
  8. Java- Different Operators Types
  9. Java- Flow Control Statements
  10. Java- Array
  11. Java- Exception Handling
  12. Java- ‘throw’ vs ‘throws’ Keyword
  13. Java- RegEx
  14. Java 12- New Advanced Features

Java OOPs concepts

  1. Java- OOPs Introduction
  2. Java- Classes & Objects
  3. Java- Constructor & Overloading
  4. Java- Method Overload vs Override
  5. Java- Access Modifiers
  6. Java- Abstraction
  7. Java- Inheritance
  8. Java- Interfaces
  9. Java- Interface Default Methods
  10. Java- Nested Inner Classes

Java Advanced

  1. Java- Applet vs Application
  2. Java- HashMap Collections
  3. Java- ArrayList
  4. Java- LinkedList
  5. Java- Reverse Linked List
  6. Java- Functional Interfaces
  7. Java- HashSet
  8. Java- HashMap vs HashSet
  9. Java- LinkedHashSet 
  10. Java- TreeSet
  11. Java- TreeMap

Java Exercise

50+ Java Coding Questions [Practice]

Java Projects

Patient Billing Software

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard