You are on page 1of 15

Name: Kandarp Kotadia

Enrolment No: 91900103034


Branch: Computer Engineering
Class: 4TC1
Subject Code: 01CE0403
Subject Name: Object Oriented Programming with Java

Program Title: Write a java Program to print “Hello World”.

Program Code:

package SEM4;
class P01 {

public static void main(String[] args) {

System.out.println("Hello World!");
}
}

Program Output:

Program Title: Write a console program to define and initialize a variable of type
byte to 1, and then successively multiply it by 2 and display its value 8 times.
Explain the reason for the last result.

Program Code:

package SEM4;
class P02 {
public static void main(String[] args) {
byte a = 1;
for(int i=0;i<8;i++){
a = (byte)(a*2);
System.out.println(a);
}
}
}
Program Output:

Program Title: Write a program that defines a floating-point variable initialized with
a dollar value for your income and a second floating-point variable initialized with
a value corresponding to a tax rate of 35 percent. Calculate and output the
amount of tax you must pay with the Rs. and paisa stored as separate integer
values (use two variables of type int to hold the tax, perhaps taxRs and taxPaisa).

Program Code:

package SEM4;
class P03{
public static void main(String[] args) {

float a = (float)10000.5;
System.out.println("a: "+a);
float tax = (float)((10000.5*35)/100);
double Rs = tax*73;
int taxRs = (int)Rs;
int taxPs = (int)((Rs % taxRs) * 100);
System.out.println("Tax in Rs: " + taxRs);
System.out.println("Tax in Paisa: " + taxPs);
}
}

Program Output:

Program Title: Write a program that calculates percentage marks of the student if
marks of 6 subjects are given.
Program Code:

package SEM4;

import java.util.Scanner;
class P04 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a[] = new int[6], sum = 0;
double total = 0;

for (int i = 0; i < 6; i++) {


a[i] = s.nextInt();
sum += a[i];
}

// By command line
/*
for(int i=0;i<6;i++){
int x = Integer.parseInt(args[i]);
sum+=x;
}
*/
total = (double) sum / 600 * 100;
System.out.println("Percentage: " + total);
}
}

Program Output:

Program Title: Write a program to display a random choice from a set of six
choices for breakfast (you could use any set; for example, scrambled eggs,
waffles, fruit, cereal, toast, or yogurt).
Program Code:

package SEM4;
import java.util.Scanner;
import java.lang.*;
class P05 {

public static void main(String[] args) {


Scanner s = new Scanner(System.in);

int a = (int)(Math.random()*7);

switch(a){
case 0:
System.out.println("Scrambled Eggs");
break;
case 1:
System.out.println("waffles");
break;
case 2:
System.out.println("fruit");
break;
case 3:
System.out.println("cereals");
break;
case 4:
System.out.println("toast");
break;
case 5:
System.out.println("yogurt");
break;
}
}
}

Program Output:

Program Title: When testing whether an integer is a prime, it is sufficient to try to


divide by integers up to the square root of the number being tested. Write a
program to use this approach.
Program Code:

package SEM4;

import java.util.Scanner;
public class P06 {
public static void main(String[] args){
Scanner s = new Scanner(System.in);

int a = s.nextInt();
int sr = (int)Math.sqrt(a);
for(int i=2; i<sr; i++){
if(a==2 || a==3 || a==5 || a==7){
System.out.println("prime");
break;
}
if(a % i == 0){
System.out.println("not prime");
break;
}else{
System.out.println("prime");
break;
}
}
}
}

Program Output:

Program Title: A lottery requires that you select six different numbers from the
integers 1 to 49. Write a program to do this for you and generate five sets of
entries.

Program Code:

package SEM4;

import java.util.Scanner;
public class P07 {
public static void main(String args[]){
Scanner s = new Scanner(System.in);
int a[] = new int[5];
for(int i=0; i<5; i++){
a[i] = (int)(Math.random()*49+1);
for(int sum:a){
if(sum == a[i]){
continue;
}
}
}
for(int j:a){
System.out.println(j);
}
}
}

Program Output:

Program Title: Write a program to generate a random sequence of capital letters


that does not include vowels.

Program Code:

package SEM4;
import java.util.Scanner;
public class P08 {
public static void main(String args[]){
Scanner s = new Scanner(System.in);
System.out.print("Enter no of alphabets you want to generate: ");
int no = s.nextInt();
for(int i=0; i<no;){
int c = 65 + (int)(Math.random()*(90-65)+1);
if(c == 65 || c == 69 || c == 73 || c == 79 || c == 85){
}else{
System.out.println((char)c);
i++;
}
}
}
}
Program Output:

Program Title: Write an interactive program to print a string entered in a pyramid


form. For instance, the string “stream” has to be displayed as follows:
S
St
Str
Stre
Strea
Stream

Program Code:

package SEM4;

public class P09 {


public static void main(String a[]) {

char ch[] = {'S', 'T', 'R', 'E', 'A', 'M'};


String s = new String(ch);
int i, j, k, n;
n = s.length();

for (i = 0; i <= n; i++) {


for (k = 0; k < i; k++) {
System.out.print(s.charAt(k) + " ");
}
System.out.println(" ");
}
}
}

Program Output:
Program Title: Create an array of String variables and initialize the array with the
names of the months from January to December. Create an array containing 12
random decimal values between 0.0 and 100.0. Display the names of each month
along with the corresponding decimal value. Calculate and display the average of
the 12 decimal values.

Program Code:

package SEM4;

import java.util.Scanner;
public class P10 {
public static void main(String[] args){

Scanner s = new Scanner(System.in);

String[] months = {"January", "February", "March", "April", "May", "June",


"July", "August", "September", "October", "November", "December"};
double[] no = new double[12];
double sum = 0.0;

for(int i=0; i<12; i++){


no[i] = Math.random()*100.00;
System.out.print(no[i] + ": ");
System.out.println(months[i]);
sum += no[i];
}
System.out.println("Average: " + sum/12);

}
}

Program Output:
Program Title: Write a program to accept a line and check how many consonants
and vowels are there in line.

Program Code:

package SEM4;

import java.util.Scanner;
public class P11 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the string");

String a = s.nextLine();
int i, vowel = 0, consonant = 0, scnt = 0;
char c;

for (i = 0; i < a.length(); i++) {


c = a.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
vowel++;
}else {
consonant++;
}
}

System.out.println("There are " + vowel + " vowel");


System.out.println("There are " + consonant + " consonant");
}
}

Program Output:

Program Title: Write a program to find length of string and print second half of the
string.

Program Code:

package SEM4;
import java.util.Scanner;
public class P12{
public static void main(String[] args) {
Scanner Sc = new Scanner(System.in);
System.out.println("ENTER THE STRING: ");
String s = Sc.nextLine();
int n = s.length();
System.out.println("STRING LENGTH IS = " + s.length());
String half = s.substring(s.length() / 2);
System.out.print("THE SECOND HALF OF THE STRING IS :" + half);
}
}

Program Output:

Program Title: Write a program to find whether a given number or string is


palindrome or not.

Program Code:

package SEM4;
import java.util.Scanner;

public class P13{


public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("ENTER THE STRING : ");
String s1 = s.nextLine();
int len = s1.length();
char ch;
String s2 = "";
for (int i = (len - 1); i >= 0; i--) {
ch = s1.charAt(i);
s2 += ch;
}
if (s1.equalsIgnoreCase(s2)) {
System.out.println("IT IS PALINDROME.");
} else {
System.out.println("IT IS NOT PALINDROME.");
}
}
}
Program Output:

Program Title: Create a class which ask the user to enter a sentence, and it should
display count of each vowel type in the sentence. The program should continue
till user enters a word “quit”. Display the total count of each vowel for all
sentences.

Program Code:

package SEM4;

import java.util.Scanner;
public class P14 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a=0, e=0, i=0, o=0, u=0;
while (true) {
int A = 0, E = 0, I = 0, O = 0, U = 0;
String s = new String();
System.out.print("Enter the string: ");
s = sc.nextLine();
if (s.equals("quit")) {
break;
}
for (int j = 0; j < s.length(); j++) {
char ch = s.charAt(j);
if (ch == 'a' || ch == 'A') {
A++;
}
if (ch == 'e' || ch == 'E') {
E++;
}
if (ch == 'i' || ch == 'I') {
I++;
}
if (ch == 'o' || ch == 'O') {
O++;
}
if (ch == 'u' || ch == 'U') {
U++;
}
}
a += A;
e += E;
i += I;
o += O;
u += U;
System.out.println("Vowel A : " + A);
System.out.println("Vowel E : " + E);
System.out.println("Vowel I : " + I);
System.out.println("Vowel O : " + O);
System.out.println("Vowel U : " + U);
}
System.out.println("Final count of Vowel A : " + a);
System.out.println("Final count of Vowel E : " + e);
System.out.println("Final count of Vowel I : " + i);
System.out.println("Final count of Vowel O : " + o);
System.out.println("Final count of Vowel U : " + u);
}
}

Program Output:

Program Title: Define an abstract base class Shape that includes protected data
members for the (x, y) position of a shape, a public method to move a shape, and
a public abstract method show() to output a shape. Derive subclasses for lines,
circles, and rectangles. You can represent a line as two points, a circle as a center
and a radius, and a rectangle as two points on diagonally opposite corners. Test
the classes by objects of the derived classes, and then invoking the show()
method.
Program Code:

package SEM4;

abstract class Shape {


protected int x, y;
Shape(int x1, int y1) {
x = x1;
y = y1;
}

abstract void show();


abstract void move();
}

class Line extends Shape {


int x1, y1;
Line(int x, int y, int x2, int y2) {
super(x, y);
x1 = x2;
y1 = y2;
}

public void move() {


show();
}

public void show() {


System.out.println("x : " + x + ", y : " + y);
System.out.println("x1 : " + x1 + ", y1 : " + y1);
}
}

class Circle extends Shape {


int radius;
Circle(int x, int y, int r) {
super(x, y);
radius = r;
}

public void move() {


show();
}

public void show() {


System.out.println("x : " + x + ", y : " + y);
System.out.println("radius = " + radius);
}
}

class Rectangle extends Shape {


int l, b;
Rectangle(int x, int y, int x1, int y1) {
super(x, y);
l = x1;
b = y1;
}

public void move() {


show();
}

public void show() {


System.out.println("x : " + x + ", y : " + y);
System.out.println("L : " + l + ", B : " + b);
}

class P15 {
public static void main(String[] args) {
Line l = new Line(1, 2, 3, 4);
Circle c = new Circle(4, 5, 6);
Rectangle r = new Rectangle(7, 8, 9, 10);

Shape s;
System.out.println("CIRCLE : ");
s = c;
s.show();
System.out.println("\n");
System.out.println("LINE : ");
s = l;
s.show();
System.out.println("\n");
System.out.println("RECTANGLE : ");
s = r;
s.show();
}
}
Program Output:

You might also like