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

What do you want to Learn Today?

  • Programming
    • C/C++
    • Python
    • Java
    • HTML CSS
    • SQL
  • CSE Subject
    • Compiler Design
    • Computer Network
    • COA
    • Data Structure
    • DBMS
    • Operating System
    • Theory of Automata
    • Web Technology
  • Linux
  • Trend
    • Artificial Intelligence (AI)
    • Big Data
    • Cloud Computing
    • Machine Learning (ML)
  • GATE CSE 2020
    • Self Study Plan
    • Complete Syllabus
    • FREE Test Series
    • Topper Interview (AIR 15)
    • Recommended Books by Topper
  • Career
    • Placement Interview
    • Jobs
    • Aptitude
    • Quiz
  • Material
    • Recommended Books
    • Software Installation
  • Contribute to Us
    • Write for Us
    • Submit Source Code or Program
    • Share Interview Experience
  • Tools

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

Pranati Paidipati/24 Sep, 18/933/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

Related Posts

C / C++CodeJAVAPython

[Explained in Detail] if else nested Programming Example in C, Java and Python

CodeJAVA

Java Program for Multiple Inheritance using Interface Example Explained

CodeJAVA

How to Achieve Abstraction in Java | Real Programming Example

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++.

Leave a Reply Cancel reply

Interview Experiences

AccentureAgriChain Akamai Amazon Amdocs American Express Attra Infotech BARC Barclays BlockGrain BYJUS Cisco Cognizant Coupon Dunia Credit Suisse DE Shaw Druva Experis IT Eze Software Factset Fiorano General Electric Incture Technologies Infosys Kasmo Cloud Microsoft MindTree Mu Sigma Numerify Opteamix Oracle Persistent Pole to Win Qualcomm Reliance Riverbed Syntel TCS Tech Mahindra Teradata Terralogic Virtusa Wipro

Interview Questions



You can share your interview experience.

Don’t Miss !

Latest Articles

Cisco Online Test Pattern and Interview Questions for Freshers


[7 Best Tips] How to Make Daily Study Timetable and Stick to It?


Persistent Written Test and Technical Coding Questions [Paper Pattern]


10 Top Website Design Tips for Small Business to Boost Up Your Sales


Main Difference Between remove del and pop in Python List


Tech Mahindra Placement (Online Test | Essay Writing | Interview Questions)


Importance of Career Exploration for High-School Students | Why?


Favorite Topic

AI algorithm array bigdata bit manipulation career Code Computer Network cpp data analytics database data scientist Data Structure db DBMS difference between Django education GATE GATE Topper Interview HTML ibm IBM ISDL interview IP address Java JavaScript Jobs Linked List linux linux cmd nit OOPs Concept os Programming Python python list Qualcomm SOAP sorting stack string vim webbrowser web development

Contribution to Community

  • Contribute to CSEstack Portal
  • Submit Your Source Code or Program
  • Share Your Interview Experience
  • CSEStack Leadership Board
  • Campus Ambassador Program by CSEstack

About CSEstack Portal

  • About CSEStack:
  • Contact Us
  • CSEStack Campus Ambassador
  • Recommended Books by Expert

© 2019 – CSEstack.org. All Rights Reserved.

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