0% found this document useful (0 votes)
266 views15 pages

Java Welcome Program Examples

The document contains 10 questions related to Java programming. Each question provides sample Java code to demonstrate a concept such as: displaying a welcome message, using constructors, calculating simple interest, finding the average and sum of user-input numbers, using inheritance and the super keyword to calculate area and perimeter of shapes, using abstract classes and methods for shapes, displaying the current date and time, creating and using packages, and adding elements to an ArrayList. The code samples are followed by the expected output for each question.

Uploaded by

Abhishek Shaha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
266 views15 pages

Java Welcome Program Examples

The document contains 10 questions related to Java programming. Each question provides sample Java code to demonstrate a concept such as: displaying a welcome message, using constructors, calculating simple interest, finding the average and sum of user-input numbers, using inheritance and the super keyword to calculate area and perimeter of shapes, using abstract classes and methods for shapes, displaying the current date and time, creating and using packages, and adding elements to an ArrayList. The code samples are followed by the expected output for each question.

Uploaded by

Abhishek Shaha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Q.

1) Write a program to java to display java welcome


massage .
Java code :
class simple{
static public void main(String args[])
{
[Link](" Welcome to Java ....! ");
}
}

Output :
Q.2) Write a program in java that demonstrate
constructor i) Default ii) Parameterized Constructor .
Java code :
class student
{
int i ;
String n;
//Constructor with no parameter
private student()
{
i = 418;
}
// constructor accepting single value
student(String name)
{
n=name;
[Link]("Student name:" +name);
}
public static void main(String[] args)
{

student obj1 = new student();


[Link]("Roll number:" +obj1.i);

//call constructor by passsing a single value


student obj2 = new student("omkar");

}
}

Output :

Q 3.) Write a program to calculate simple interest input


by the user .
Java code :
import [Link];
class Interest
{
float SimpleInterest;
public void si (float p , float r , float t)
{
SimpleInterest = p*r*t/100;
[Link]("Simple inteest:" +SimpleInterest);
}
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
float p, r, t;
[Link]("Enter the Principal : ");
p = [Link]();
[Link]("Enter the Rate of interest : ");
r = [Link]();
[Link]("Enter the Time period : ");
t = [Link]();
Interest obj = new Interest();
[Link](p,r,t);
}
}

Output :
Q4 .) Write a program to find the average and the sum of the n
number using user input
Java code :
import [Link];
class Avareage
{
public static void main(String[] args)
{
int n,sum=0;
float ave;
Scanner sc = new Scanner([Link]);
[Link]("Enter no. of element you want in array:");
n = [Link]();
int a[] = new int[n];
[Link]("Enter All the element you want in array:");
for(int i = 0; i < n ; i++)
{
a[i] = [Link]();
sum = sum + a[i];
}
[Link]("Sum of all element:" +sum);
ave = (float)sum / n ;
[Link]("Avareage of all element:" +ave);
}
}
Output :

Q5) To create simple class to find out area and perimeter of


rectangle and box using super keyword.
Java code :
import [Link];
class simple
{
float area,p,SurfaceArea,perimeter;
void Area(float l , float w){
area = l * w ;
}
void perimeter(float l , float w ){
p = 2*( l + w );
}
void box(float l , float w , float h)
{
SurfaceArea=2*l*w + 2*l*h + 2*w*h;
}
void box1(float l , float w , float h)
{
perimeter=4*(l+h+w);
}
}
class rectangle extends simple{
void parameters(float l , float w , float h){
[Link](l,w);
[Link]("Area of rectangle is :" +[Link]);
[Link](l,w);
[Link]("Perimeter of rectangle:" +super.p);
[Link](l,w,h);
[Link]("Surface Area of box is :" +[Link]);
super.box1(l,w,h);
[Link]("Perimeter of box is :" +[Link]);
}
}
class simpleSuper1{
public static void main(String[] args){
float l,w,h;
Scanner sc = new Scanner([Link]);
[Link]("Enter the length:");
l = [Link]();
[Link]("Enter the width:");
w = [Link]();
[Link]("Entern the hight:");
h = [Link]();
rectangle r1 = new rectangle();
[Link](l,w,h);
}
}

Output:
Q6) To design a shape class using abstract method and classes ?
Circle , rectangle , square .
Java Code :
abstract class Shape{
abstract void draw();
}
class Rectangle extends Shape
{
void draw()
{
[Link]("Drawing rectangle");}
}
class circle extends Shape
{
void draw()
{
[Link]("Drawing circle");}
}
class square extends Shape
{
void draw()
{
[Link]("Drawing square");}
}
class Abstraction
{
public static void main(String args[])
{
Shape s=new Rectangle();
[Link]();
Shape s1=new circle ();
[Link]();
Shape s2=new square();
[Link]();
}
}

Output :

Q 7 ) Write a program using calendar that display current year ,


month ,date.
Java code :
import [Link];
import [Link].*;
class format
{
public static void main(String[] args)
{
Date dnow = new Date();
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
[Link]("Current date:" +[Link](dnow));
}
}

Output :

Q 8) Write a program using calendar that display date using


yy/mm/dd/ and day of week ,week of month ,
hours:minuts:seconds:milliseconds .
Java code :
import [Link].*;
import [Link].*;
class cal
{
public static void main(String[] args)
{
Calendar cal = [Link]();
Date d = new Date();
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
[Link]("Current date:" +[Link](d));
[Link]("Current year: " + [Link]([Link]));
[Link]("Current month: " + [Link]([Link]));
int dayofweek = [Link](Calendar.DAY_OF_WEEK);
[Link]("Day of week:" +dayofweek);
int wk=[Link](Calendar.WEEK_OF_MONTH);
[Link]("Week of Month :"+wk);
Date dnow = new Date();
SimpleDateFormat ft = new SimpleDateFormat("HH:mm:ss:SS");
[Link]("Currrent time:" +[Link](dnow));
}
}

Output :
Q 9) Write a program to create package that display welcome to
package .
Java code :
package mypack;
class simple
{
public static void main(String[] args)
{
[Link]("Wlcome to package...!");
}

Output :
Q 10 ) Write a program to create package array list using
10 elements .
Java code :
package Demo;
import [Link];
class BuiltInPackage
{
public static void main(String[] args)
{
ArrayList<Integer> list = new ArrayList<>();
[Link](2);
[Link](5);
[Link](55);
[Link](6);
[Link](24);
[Link](36);
[Link](78);
[Link](99);
[Link](41);
[Link](25);
[Link]("Array:" +list);
}
}

Output :

Common questions

Powered by AI

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 .

You might also like