Professional Documents
Culture Documents
Program Code:
package SEM4;
class P01 {
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;
// 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 {
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:
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 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 Code:
package SEM4;
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){
}
}
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;
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 Code:
package SEM4;
import java.util.Scanner;
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;
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: