www.esnips.com/user/vapremaims Page 1 OBJECT ORIENTED PROGRAMMING THROUGH JAVA - I JAVA PHASE 1 PROGRAMS Task 1 Your first task is to read the user's name and print it as "Hello <<username>>. Welcome!". Eg: If user's name is Surya; you should be displaying "Hello Surya. Welcome!" // This program read the input from user import java.io.*; class Read { public static void main(String[] args) throws IOException{ InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); //To read the user name System.out.println("Enter the user name"); String name=br.readLine(); System.out.println("Hello "+name); } } Task 1 Output Module 1 Introduction to Java
65
Embed
Java Phase 1 Object Oriented Programming Through Java
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
www.esnips.com/user/vapremaims Page 1
OBJECT ORIENTED PROGRAMMING THROUGH JAVA - I
JAVA PHASE 1 PROGRAMS
Task 1
Your first task is to read the user's name and print it as "Hello <<username>>. Welcome!".
Eg: If user's name is Surya; you should be displaying "Hello Surya. Welcome!"
// This program read the input from user
import java.io.*;
class Read {
public static void main(String[] args) throws IOException{
Try compiling and running the following code snippets and report on the output and the reason for the output (in .doc or .txt file)
Code snippet 1: int num=2147483647; System.out.println("The number is "+num); num=num+1; System.out.println("The number is "+num); Code snippet 2: float num; System.out.println(num=20); Code snippet 3: int age=24.3; System.out.println("The age is "+age); What can be done to overcome this issue?
class Task1a
{
public static void main(String args[])
{
int num=2147483647;
System.out.println("The number is "+num);
num=num+1;
System.out.println("The number is "+num);
}
}
Task 1 Output Module 1 Section 2 Data Types and Operators
Code snippet 3: int age=24.3; System.out.println("The age is "+age); What can be done to overcome this issue?
class Task1d
{
public static void main(String args [])
{
int age=24.3; // this is an error
System.out.println("The age is "+age);
}
}
www.esnips.com/user/vapremaims Page 4
Output
Report on OverCome Issue
This code is not compiled because here floating type value assigned integer variable age so it has given error. To overcome this problem age must be declared as float type (or) integer value is assigned to int type age.
Task 2
You have to extend your first program (Hello world program in previous section) and read in the age and credit card number of the user and print them to the screen. You should store the age and credit card number in appropriate data types.
// To read age and credit number and print in appropriate datatypes
import java.io.*;
class Credit
{
public static void main(String args[]) throws IOException
System.out.println("Credit card number is:"+creditno);
}
}
Task 2 Module 1 Section 2 Data Types and Operators
www.esnips.com/user/vapremaims Page 6
Module 2
Task 1
Once you have perused the reading material, you can now work on the following task. You will need to provide the output of the following code snippets.
int i=10; System.out.println(i++ +"::"+ ++i); int j=20; int k=30; int l=40; int m=50; int n=60; System.out.println(j+k-l/m*n); boolean result=true; System.out.println(result!=false);
class Module2Task1
{
public static void main(String args[])
{
int i=10;
System.out.println(i++ +"::"+ ++i); //it will print as 10 :: 12
int j=20;
int k=30;
int l=40;
int m=50;
int n=60;
System.out.println(j+k-l/m*n);// it will print as 50
boolean result=true;
System.out.println(result!=false);// it will print as true
}
www.esnips.com/user/vapremaims Page 7
}
Task 1 Module 2 Section 2 Data Types and Operators
Task 2
You are supposed to find out the area of a circle. You have to read the radius from the user and print the area.
Hint: Area of circle = pi*radius*radius pi=22/7 is a constant Note: We can define constants in Java using the keyword final which you will be introduced to later.
Task 2 Output Module 2 Section 2 Data Types and Operators
www.esnips.com/user/vapremaims Page 9
Section 3 Control Statements in Java
Module 1
Task 1
Raju's parents are celebrating their marriage anniversary. They have arranged a small party tonight. They are expecting their colleagues and Raju's friends to attend the party. They have planned to serve 'Coke' and 'Badam Milk' to these guests. But they would like to serve 'Badam Milk' to teenagers and 'Coke' to adults. Please help them in finding teenagers and adults based on age. Write a Java program to find out the adults and teenagers based on their age.
Note: Teenagers are those whose age is between 13 and 19 (both inclusive).
Step by Step guide
Read the age as input from the user Check the age with the conditions mentioned Display the appropriate messages (Eg: Adult or Teenager) and also a message regarding the
drink (Eg: Badam Milk or Coke)
Note: We can come with a condition where some guests are neither teenagers nor adults. What do we do then? Just serve them water and call them Children :-)
//Finding a guest whether guest is adult or teenager or children depending on the age
System.out.println("Guest is TEENAGER, serve BADAM MILK");
}
else if(age>19)
{
System.out.println("Guest is ADULT, Serve COKE");
}
else
System.out.println("Guest is children, serve water");
}
}
Task 1 Output Module 1 Section 3 Control Statements in Java
www.esnips.com/user/vapremaims Page 11
Task 2
Your college management is planning to conduct a cricket tournament as part of their college fest and have planned to arrange a digital score board. This digital score board should display greetings/message based on the batsman's score. If the batsman's score is:
>=100 - Message should be displayed "Congratulations on your magnificent century" 80 - 99 - Message should be displayed "If you had played carefully, you would have
seen another milestone in your career" 70 - 79 - Message should be displayed "Good performance, keep it up" 50 - 69 - Message should be displayed "Well played" 10 - 49 - Message should be displayed "Nicely Played but bad luck..." 0 - 9 - Message should be displayed "Oops! hard luck"
//Displaying cricket score board messages depending on the bats man performance
Additional Task Output Module 1 Section 3 Control Statementsn in Java
www.esnips.com/user/vapremaims Page 15
Module 2 Section 3 Control Statements in Java
Module 2
Task 1
Samar your friend has an interesting task for you. He has with him a list of student marks (all marks are out of 100). He now wants to convert these marks from number to word format (i.e. 56 in number format is Fifty six in word format). He knows that you have knowledge of conditional statements and he has approached you to help him. So, now you work on designing a Java program that will help Samar. For simplicity sake, lets assume the following:
Samar enters the marks (in numbers) All marks are integer values only No student gets negative marks No student gets more than 100 marks
Task 1 Output Module 2 Section 3 Control Statements in Java
www.esnips.com/user/vapremaims Page 20
Task 2
You need to implement the calculator functionality in Java. You will need to give an option for the user to perform different functions like "Addition", "Subtraction", "Multiplication" and "Division". Please note that the user will be able to perform only one operation from the above mentioned operations. Hint: Use switch-case and if-then-else conditions
In the next module you will be introduced to "Loops" wherein you will learn how to perform repetitive operations. So, save this program in a location from where you can retrieve it later.
Task 2 Output Module 2 Section 3 Control Statements in Java
www.esnips.com/user/vapremaims Page 22
Module 3 Section 3
Task 1
Write a program that prints the first 50 UGLY numbers. Note: Ugly numbers are numbers whose only prime factors are 2, 3 or 5. For example 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15,…… are ugly numbers. By convention, 1 is considered as an ugly number. You can use any one of the iterative statements
class Ugly
{
public static void main(String[] args)
{
int c=0;
int i=1;
System.out.println("1");
while(c<=50)
{
if(i%2==0||i%3==0||i%5==0)
{
System.out.println(i );
c=c+1;
}
i=i+1;
}
}
}
www.esnips.com/user/vapremaims Page 23
Task 1 Output Module 3 Section 3 Control Statements in Java
www.esnips.com/user/vapremaims Page 24
Additional Tasks
Write a program to print the multiplication table (till 20) of a given number.
Eg: If number is 2 Output should be: 2*1 = 2 2*2 = 4 . . . 2*20 =40
import java.io.*;
class Multi
{
public static void main(String args[]) throws IOException
Additional Task Output Module 3 Sectio 3 Control Statements in Java
www.esnips.com/user/vapremaims Page 26
Module 4 Section 3 Control Statements in Java
Task 1
Write a calculator program using switch-case (you can use the already designed program in the previous section). The program should provide 4 operations - add, subtract, multiply and divide - and ask the user to select an option. The program should perform the operation selected by user and display the result. If user selects an option other than the options specified, a default error message should be displayed that the option selected does not exist. The program should take in user input till the user types in 'q' as an option.[Once user types in 'q', the program should quit].
import java.io.*;
class Cal2
{
public static void main(String args[]) throws IOException
Task 1 Output Module 4 Section 3 Control Statements in Java
www.esnips.com/user/vapremaims Page 29
Task 2
Samar again comes to you with a problem.
Problem statement: My teacher has asked me come with a list of numbers that are the sum of their own digits to the power of the number of digits. I am totally clueless on how to proceed. To make things easy, my teacher has provided me with an example of such a number.
Eg1 : Lets consider the number 153 Number of digits in 153 is 3 We can see that 153 = 13 + 53 + 33 Thus 153 is one such number that is the sum of its own digits to the power of the number of digits. Eg2: Lets consider the number 1634. Number of digits is 4 We can see that 1634 = 14 + 64 + 34 + 44 Thus 1634 is one such a number that is the sum of its own digits to the power of the number of digits.
My teacher has also told me that such numbers are referred to as Armstrong numbers. I am supposed to come with the list of all such numbers below 1,000,000. Boss! please help me.
import java.io.*;
class Teach
{
public static void main(String args[]) throws IOException
Additional Task 2 Output Module 3 Control Statements in Java
www.esnips.com/user/vapremaims Page 36
Object Oriented Programming Section 4
Module 1
Task 1
Your old friend who is working in the admissions department of your college comes to you with a problem.
ProblemStatement: I am in charge for the students admissions every year. I now want to computerise this admission process and want you to help me in this regard. Create an application that would allow me to do the following:
Create a student record Display the student record Remove the student record
Consider only the student's name and age when creating a student record (for now).
Note: You will be creating only one student record in this case. In the next section, you will know how to create more records (an array of records/objects).
import java.io.*;
import java.util.*;
class Student1
{
String Sname;
String Grp;
String Sex;
String Age;
Student1()throws IOException
{
BufferedReaderbr= new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter Student name : ");
Sname=br.readLine();
www.esnips.com/user/vapremaims Page 37
System.out.print("\nEnter Grp : ");
Grp=br.readLine();
System.out.print("\nEnter Sex : ");
Sex=br.readLine();
System.out.print("\nEnter Age : ");
Age=br.readLine();
}
public void disdetails()
{
System.out.println("Student Name:"+Sname);
System.out.println("Student Grp:"+Grp);
System.out.println("Student Sex:"+Sex);
System.out.println("Student Age:"+Age);
}
public void removedetails()
{
}
}
class Student
{
public static void main(String[] args) throws IOException
{
Student1 s=new Student1();
s.disdetails();
s.removedetails();
}
www.esnips.com/user/vapremaims Page 38
}
Task 1 Output Module 1 Section 4 OOPS - I
www.esnips.com/user/vapremaims Page 39
Task 2
In this task, we will introduce you to the Java API (Please right click on this link and open in a new window/tab).
You can see that the Java API contains all the classes available in the Java language. The API gives you a snapshot of the classes and the methods in them.
Your task is to generate a list of 10 random numbers and print them.
Hint: Which class in the API would you use to generate the random numbers? We have provided you a hint in the question itself :-)
Create a class Clock that has the following attributes:
Hour (integer) Minutes (integer) Seconds (integer) isAM (boolean) - true if time is before 12:00 Noon and false if time is after 12:00
Noon
These attributes are represented by data fields. Class Clock should contain the following constructors:
no parameters (sets clock to midnight) all attributes specified (hour, minute, second, isAM)
Class Clock supports the following public methods:
getHours() - returns the Hours getMinutes() - returns the Minutes getSeconds() - returns the Seconds getIsAM() - returns AM status
Class Clock also supports a setTime() method that reads all four attributes from user.
Note: Take the following into consideration
The time is in 12 hour format (So isAM needs to be taken from user). The user may or may not set the time at runtime (i.e. user may or may not enter
argument values) o In this case the setTime() method should be invoked to read the time.
www.esnips.com/user/vapremaims Page 41
Additional Tasks
Task 1
Implement the complex number class. At a minimum it should have a constructor,and methods to add, subtract, and multiply two complex numbers, and to return the real and imaginary parts. (complex number = x + iy)
Hint: Ask user to input real and imaginary part individually
For example: Suppose the two imaginary numbers are 2+5i and 6-3i, you should be taking input as follows
Enter the real part of the first number (a): 2
Enter the imaginary part of the first number (b): 5
Enter the real part of the second number (c): 6
Enter the imaginary part of the second number (d): -3
Now perform addition, subtraction and multiplication operations on these two complex numbers.
public class Complex {
int x;
int y;
public Complex (int x, int y) {
this.x = x;
this.y = y;
}
public Complex(){
}
public Complex addComplex(Complex a) {
Complex c = new Complex();
c.x = this.x + a.x;
www.esnips.com/user/vapremaims Page 42
c.y = this.y + a.y;
return c;
}
public Complex subtractComplex(Complex a) {
Complex c = new Complex();
c.x = this.x - a.x;
c.y = this.y - a.y;
return c;
}
public Complex multiplyComplex(Complex b){
Complex c = new Complex();
c.x = this.x * b.x - this.y * b.y;
c.y = this.x * b.y + this.y * b.x;
return c;
}
public void printComplex(){
System.out.println(this.x+" i "+this.y);
}
public static void main(String[] args) {
Complex a = new Complex(5,4);
Complex b = new Complex(5,3);
Complex c=a.addComplex(b);
c.printComplex();
c=a.subtractComplex(b);
c.printComplex();
c=a.multiplyComplex(b);
c.printComplex();
www.esnips.com/user/vapremaims Page 43
}
}
Additional Task OutputModule 2 Section 4 OOPS - I
www.esnips.com/user/vapremaims Page 44
Section 5 Java Coding Standards
Module 1
No Tasks in this Module
www.esnips.com/user/vapremaims Page 45
Section 6 Arrays in Java OOPS JAVA Phase 1
Module 1
Task 1
Write a program to generate 10 random numbers and store it in a array. Display the maximum and minimum numbers in the array.
Note: You have worked on generating the random numbers. you can use the same program
import java.util.*;
class Task1
{
public static void main(String args[])
{int i=0;
int a[]=new int[11];
Random r=new Random();
System.out.println("the 10 random numbers are");
while(i<10)
{
a[i]=(r.nextInt(100));
System.out.println(+a[i]);
i=i+1;
}
Arrays.sort(a);
System.out.println("The Minimum value in the array:"+a[1]);
www.esnips.com/user/vapremaims Page 46
System.out.println("The Maximum value in the array:"+a[a.length-1]);
Mr. Varun is running an online gaming website. He initially started his site by putting Sudoku puzzles. He want you to write a program to check whether the answer to the puzzle is correct or not.
To make things easy, initialize your 2D array with the data given below.
Sudoko: Given a 9-by-9 array of integers between 1 and 9, check if it is a valid solution to a Sudoku puzzle: each row, column, and block should contain the 1 to 9 integers exactly once.
Write a program to develop a menu of String operations like the program should provide 5 operations(uppercase, lowercase, display each word, search for a substring, replace white space with $ symbol)
Take the string(more than 3 words) and choice of user as input and the program should perform the operation selected by user and display the result.
Hint: Use Java API to solve this problem
Sample Test case: Enter string of your choice: "Welcome to object oriented programming in java"
---String Operations Menu--- 1. Convert to UpperCase 2. Convert to LowerCase 3. Display each word in the string 4. Search for a substring 5. Replace all the white spaces in the string with $ symbol Enter your choice: 1 Output: WELCOME TO OBEJCT ORIENTED PROGRAMMING IN JAVA Enter your choice: 2 Output:"welcome to object oriented programming in java" Enter your choice: 3 Output: welcome to object oriented programming in java Enter your choice: 4 Enter your key to search: "object" Output: "Key found" Enter your choice: 5 Output: "welcome$to$object$oriented$programming$in$java"
Write a program to encrypt a message that your boss is sending to a client. And decrypt that message after the client is received using the following algorithm
To make it simple, take a message (string of words, atleast 3), key as input and print the encrypted and decrypted messages.
Step-by-step guide
Shuffle the words in the message(say 1st word appears in the last and so on)
ex: msg: "Welcome to java programming" shuffle as "programming java to welcome"
Take the Key from the user as input and encrypt by the shifting the alphabets according to the key