Java Welcome Program Examples
Java Welcome Program Examples
The primary function of the `Scanner` class in the Java programs is to handle user input. It is used to read different types of data from the console, such as integers and floating-point numbers. For example, it reads user inputs for principal, rate, and time in the simple interest calculation program, and similarly for dimensions in the programs dealing with geometric calculations .
The Java programs use the `Scanner` class extensively to incorporate user inputs, allowing dynamic interaction with the executed code. This user interaction serves multiple roles, such as taking input for principal, rate, and time in a simple interest calculator, accepting dimensions for geometric calculations, or inputting an array's size and elements for array operations. This approach highlights user-driven data handling, tailoring functionality based on user-provided parameters .
The program elegantly utilizes an abstract class `Shape`, providing an architectural blueprint that demands each concrete subclass (`Rectangle`, `Circle`, `Square`) implement the `draw()` method. This abstract method ensures consistency across the subclasses, allowing polymorphic behavior when shapes are drawn. The design aligns with good practices in object-oriented programming, promoting abstraction and code reuse while ensuring that specific details are handled at the subclass level .
The program calculates simple interest using a method `si()` within the `Interest` class, taking three parameters: principal `p`, rate `r`, and time `t`. It applies the formula `SimpleInterest = (p*r*t)/100` to determine the simple interest and then displays it using `System.out.println()`. User inputs for principal, rate, and time are taken using the `Scanner` class .
Calculating the area of a rectangle involves multiplying its length and width, whereas calculating its perimeter involves summing twice the length and twice the width. For a box, surface area calculation requires the formula `2 * (l*w + l*h + w*h)` which considers all the rectangular faces, while perimeter computation for the box involves summing four times the sum of the length, width, and height. This demonstrates how the problem scales as we consider a three-dimensional shape versus a two-dimensional one .
The Java program uses packages as a namespace management tool, providing structure by placing classes within a package. The `mypack` package contains a class that prints a welcome message, showing how a simple class can belong to a package. The `Demo` package manages an `ArrayList` by importing `java.util.ArrayList`, adding ten integers to the list, and then printing these integers. This demonstrates package utilization for organizing code and leveraging built-in Java utilities .
The program uses the `Calendar` and `Date` classes to display the current date and time. It obtains an instance of `Calendar`, retrieves the current year, month, day of the week, and week of the month using methods like `get()`. A `Date` object represents the current date, formatted using `SimpleDateFormat` to output the date as 'yyyy-MM-dd', and time using 'HH:mm:ss:SS' format for detailed time components .
The abstract class `Shape` defines a method `draw()` without providing its implementation, allowing subclasses like `Rectangle`, `Circle`, and `Square` to provide their specific implementations of the `draw()` method. This setup facilitates polymorphism, as an instance of `Shape` can hold references to different subclasses, invoking the respective `draw()` methods dynamically at runtime, depending on the instance type .
The program uses a `Scanner` object to read the integer `n`, which indicates the number of elements, and initializes an array `a[]` of this size. It iterates over the array to collect user inputs for each element while cumulatively tallying their sum in the variable `sum`. After populating the array, it calculates the average by dividing `sum` by `n` and outputs both the sum and the average .
The Java program defines a class `student` with two types of constructors: a default constructor and a parameterized constructor. The default constructor initializes a field `i` with the value 418. The parameterized constructor accepts a string parameter `name`, assigns it to an instance variable `n`, and prints the student name. An instance `obj1` is created using the default constructor, setting the roll number to 418. Another instance `obj2` is created by passing a name, 'omkar', to the parameterized constructor, demonstrating how constructors can be used to initialize objects in different ways .