0% found this document useful (0 votes)
53 views22 pages

Assignment 1

Uploaded by

ashwathbalu2005
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)
53 views22 pages

Assignment 1

Uploaded by

ashwathbalu2005
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
You are on page 1/ 22

Assignment 1

CS6308 Java Programming


Date: 14/08/2024
Reg No: 2022503023

Exercise 1: Hello friends:

Write the program and compile the code @command line to execute to
greet your friends.

Output: Hello, Alice, Bob, Charlie! Good Morning!

CODE:

public class Sample {

public static void main(String[] args) {

System.out.println("2022503023");

System.out.println("Output: Hello, Alice, Bob, Charlie! Good


Morning! \n");

Exercise 2: Error message


Find the maximum Compile time and Run time error messages of
simple one line output message.
Example

Delete any of the semicolons.

public class ComplieTime {

public static void main(String[] args) {

System.out.println("Hello,3023")

Swap the word public, static, void, main

public class ComplieTime {


static public void main(String[] args) {
System.out.println("Hello,3023");
}
}

public class ComplieTime {


static void public main(String[] args) {
System.out.println("Hello,3023");
}
}
Omit the word public, static, void, main

public class ComplieTime {


{
System.out.println("Hello,3023");
}
}

Remove the array Subscript around string

public class ComplieTime {


static void public main(String args) {
System.out.println("Hello,3023");
}
}

Replace Sring with int or float

public class ComplieTime {


static void public main(int[] args) {
System.out.println("Hello,3023");
}
}
Replace String[] as String…

public class ComplieTime {

static void public main(String args) {

System.out.println("Hello,3023");

Exercise 3: Conversation
Write a Java program to create a Conversation between Java and
Python Java: Hi, I'm Java. What's your name?
Python: I'm Python. Nice to meet you!
Java: Programmers use me for large-scale systems and performance
critical applications.
Python: I'm best for rapid development and scripting
tasks. Java: I use static typing for early error detection.
Python: I use dynamic typing for more flexibility.
Java: I run on the JVM, making me portable across many platforms.
Python: I'm portable with the Python interpreter on any
system. Java: Ideal for enterprise applications and Android
apps. Python: Perfect for web development and data analysis.
Java: My performance is strong with JVM optimizations.
Python: I excel in ease of use and quick development cycles.
Java: Use me for performance and large projects.
Python: Use me for ease and speed in
development. Java: This was great!. Bye for now!
Python: Bye!

CODE:
import java.util.Scanner;

public class Conversation {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String string;

while(true)

string = sc.nextLine();

if(string.equalsIgnoreCase("Bye"))

System.out.println("JAVA : "+ string);

break;

System.out.println("JAVA : " + string);

String python= sc.nextLine();

if(python.equalsIgnoreCase("Bye"))

System.out.println("PYTHON : "+ python);

break;

}
System.out.println("PYTHON : " +python);

Exercise4: Leap year

Write a program to check if the given year is a leap year or not.

Your input is an integer(year).The program should print a Boolean

value: True if the year is a leap year, False if not.

Constraint

year1000

Input: 2024

Output: True

Input: 2025

Output:

False
Also find the next leap year

CODE:

import java.util.Scanner;

public class LeapYear {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the year greater then or equal to 1000: ");

int s = sc.nextInt();

if (s < 1000) {

System.out.println("Enter the year greater than or equal to 1000: ");

sc.close();

return;

} else {

System.out.println("Input : " + s);

boolean isleapyear = IsLeapyear(s);

System.out.println("Output : " + isleapyear);

int nextleapyear = findnext(s);

System.out.println("next leap year : " + nextleapyear);

public static boolean IsLeapyear(int s)

{
if (s % 400 == 0) {

return true;

} else if (s % 100 == 0) {

return false;

} else if (s % 4 == 0) {

return true;

} else {

return false;

public static int findnext(int s)

int nextyear = s+1;

while(!IsLeapyear(nextyear))

nextyear++;

return nextyear;

Exercise 5:Day of the Week


Write a program that takes a date as input and prints the day of the
week that date falls on. Read the three int input as m(month), d(day)
and y(year). Use 1 of m for January, 2 for February, and so forth. For
output print 0 for Sunday, 1 for Monday and so forth. Use the following
formula for the Gregorian calendar.

CODE:

import java.util.Scanner;

public class DayOfWeek {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter month (1-12): ");

int m = scanner.nextInt();

System.out.print("Enter day (1-31): ");


int d = scanner.nextInt();

System.out.print("Enter year: ");

int y = scanner.nextInt();

int dayOfWeek = dayOfWeek(m, d, y);

System.out.println("Day of the week: " +


getDayName(dayOfWeek));

public static int dayOfWeek(int m, int d, int y) {

if (m < 3) {

m += 12;

y -= 1;

int h = (d + (13 * (m + 1)) / 5 + y + y / 4 - y / 100 + y / 400) % 7;


return h;

public static String getDayName(int dayOfWeek) {

switch (dayOfWeek) {

case 0: return "Saturday";

case 1: return "Sunday";

case 2: return "Monday";


case 3: return "Tuesday";

case 4: return "Wednesday";

case 5: return "Thursday";

case 6: return "Friday";

default: return "Invalid day"; // In case of invalid value

Exercise 6: Write a Java program to create a Indian Flag

CODE:
public class Flag {
public static void straight()
{
System.out.println("- ".repeat(20));
}
public static void ver()
{
String s = "|";
System.out.printf("%-38s%s\n",s,s);
}
public static void ver2()
{
String s = "|";
String a = "*";
System.out.printf("%-15s********%16s\n",s,s);
}
public static void var1()
{
String s = "|";
System.out.println("|");
}
public static void main(String[] args) {
straight();
for(int i=0;i<2;i++)
{
ver();
}
straight();
for (int i=0;i<=2;i++)
{
ver2();
}
straight();
for (int i=0;i<2;i++)
{
ver();
}
straight();
for(int i=0;i<=7;i++)
{
var1();
}
}
}

OUTPUT:

Exercise 7: Model OR gate

Write a program to model the AND gate using the linear combination
of

inputs formula

Y=Bias +W0X1+W1X2
where X1 and X2 are the input values, and y is the output, determine
the

values for the weights W0 and W1, and the bias term that will
correctly

model the behavior of a logical AND gate. Use the condition that
Y>0.5

results in output 1 and Y0.5 results in output 0.

CODE:

import java.util.Scanner;

class OR {

public static double Bias = -0.1;

public static void Orgate(double w0, double w1) {

int x2, x1;

System.out.println("Bias:"+Bias);

for( x1=0;x1<2;x1++)

for ( x2 = 0; x2 < 2; x2++) {

double y = Bias + w0 * x1 + w0 * x2;

if (y <= 0.5) {

y=0;

System.out.println("x0:" + x1 + " x1:" + x2 + "output:" + y + "(w0:" + w0 +


",w1:" + w1 + ")");

}
else {

y = 1;

System.out.println("x0:" + x1 + " x1:" + x2 + "output:" + y + "(w0:" + w0 +


",w1:" + w1 + ")");

public static void main(String[] args) {

for (double w0 = 0.1; w0 < 1; w0 = w0 + 0.1) {

for (double w1 = 0.1; w1 < 1; w1 = w1 + 0.1) {

Orgate(w0, w1);

OUTPUT:
Exercise 8: Write a program that converts a given integer into its

equivalent words representation. The program should handle

negative numbers and checks if the input is within the specified

range of 0 to 999.

Input : 123

Output: One Hundred and Twenty Three

CODE:

import java.util.Scanner;

public class Convertor {


public static void main(String[] args) {

String[] one_place = new String[]{"Zero", "one", "Two", "Three", "Four", "Five",


"Six", "Seven", "Eight", "Nine"};

String[] teen=new String[]


{"","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen",
"Nineteen"};

String[] ten = new String[]{"", "ten","Twenty", "Thirty", "Forty", "Fifty", "Sixty",


"Seventy", "Eighty", "Ninety"};

Scanner scan = new Scanner(System.in);

while (true) {

System.out.println("Enter the number:");

int n = scan.nextInt();

if (n > 999) {

continue;

if (n >= 0 && n < 10) {

System.out.printf("%s\n", one_place[n]);

} else if (n % 10 != 0 && n >= 21 && n <= 99) {

String temp = "";

temp += one_place[n % 10];

n = n / 10;

temp = ten[n]+" "+temp;

System.out.printf("%s\n", temp);

}else if (n % 10 == 0 && n >9 && n < 100) {

String temp = "";


n=n/10;

temp+=ten[n];

System.out.println(temp);

else if (n >= 11 && n <= 19) {

String temp = "";

n = n % 10;

temp += teen[n];

System.out.printf("%s\n", temp);

} else {

String temp = "";

int te = n % 100;

if (te >= 0 && te < 10) {

temp+=one_place[te];

if (te % 10 != 0 && te >= 21 && te < 99) {

String tei = "";

temp += one_place[te % 10];

te = te / 10;

temp = ten[te]+" "+temp;

temp += tei;

else if (te % 10 == 0 && te >9 && te < 100) {

String tei = "";


te=te/10;

tei+=ten[te];

temp+=tei;

if (te >= 11 && te <= 19) {

String tei = "";

te = te % 10;

tei += teen[te];

temp += tei;

temp = one_place[n / 100] + " Hundred and " + temp;

System.out.println(temp);

Exercise 9: Casino Game

Write a Java program to simulate a simple Casino game where the player
starts
with 1000 credit points. Each roll costs 100 credits (the bet amount). For
each roll,

If the sum of the two dice is 7 or 11, the player wins and gains 100 credits
or If the

sum of the dice is 2, 3, or 12, the player loses 100 credits or For any other
sum (4,

5, 6, 8, 9, or 10) there is no change in credits. The game continues until the


player

either goes bankrupt (reaches 0 credits) or reaches the target win amount
of 2000

credits.

CODE:

import java.util.*;

public class Casino {

public static void main(String[] args){

int player=1000;

Random ran = new Random();

while(true){

int n1 = ran.nextInt(6)+1;

int n2 = ran.nextInt(6)+1;

int n3=n1+n2;

System.out.println("Rolling the Dice...");

System.out.println("Dice sum:"+n3);

System.out.println("Current Credits:"+player);
if(n1+n2 == 7 || n1+n2 == 11){

player=player+100;

System.out.println("You win 100 credits! new Credits:"+player);

else if(n1+n2 == 2 || n1+n2 == 3 || n1+n2 ==12){

player=player-100;

System.out.println("You lost 100 credits new Credits:"+player);

if(player >= 2000){

System.out.println("You won with final credits:"+player);

break;

else if(player == 0){

System.out.println("You lost with final credits:"+player);

break;

}
OUTPUT:

You might also like