NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

//question 0

//write hello world program
// execute the program on the terminal
public class exercise_0 {
public static void main(String[] args) {
//hint System.out.println method
System.out.print("hello worldt");
System.out.println("hello world");
}

}

//question 1

/*
Write a Java program that declares three variables: an int, a double, and a String.
Assign values to these variables and print statements that include these variables.
Hint:
- use println snd string concatenation (+) when writing the statements.
or
- use printf when printing the statements.

*/

/**
* exercise_1
*/
public class exercise_1 {

public static void main(String[] args) {
int a = 10;
double b = 10.545;
String c = "Java";


System.out.println("The value of a is: " + a);
System.out.println("The value of b is : " + b);
System.out.println("The value of c is: " + c);


//use println use format such as "The value of a is: " + a
/*

* the output of print
* The value of a is: 10
* The value of b is: 10.545
* The value of c is: Java
*/
System.out.printf("The value of a is : %dn" ,a);
System.out.printf("The value of b is: %.2fn", b);
System.out.printf("The value of c is: %s ",c );

//use printf + %d for integer, %f for float, %s for string + n for new line
/*
* the output of print
* The value of a is: 10
* The value of b is: 10.55
* The value of c is: Java
*/

}
}

//question 2
my PC is Mac, so forward slash is working
/*
* write a program that prints out the following path:
* "C:newfoldertreefile.txt"
* Note: you can use the escape character '' to print out the backslash character ''
*/
public class exercise_2 {

public static void main(String[] args) {
//print the path: C:newfoldertreefile.txt
System.out.println("C:/newfolder/tree/file.txt");
}

}

//question 3
/**
* exercise_3: printf and String.format comparison
*/
public class exercise_3 {

public static void main(String[] args) {

String a = "John";
double b = 25;
String c = "Java";

//use format string
String introductionTemplate = """
Hello, my name is %s.
I am %.0f years old.
My favorite programming language is %s.
""";


//using printf
System.out.printf(introductionTemplate, a, b, c);

//Using String.format
String introduction = String.format(introductionTemplate, a, b, c);
System.out.println(introduction);
//or
System.out.println(String.format(introductionTemplate, a, b, c));
}
}

//question 4

// declare and assing string variable
// use some method such as print the length of the string
// change it to uppercase
public class exercise_4 {
public static void main(String[] args) {
String a = "John";
int length= a.length();
System.out.println("THE LENGTH OF THE STRING IS " + length);

//print the length of the string
//hint call the length() method on the string

//print the string in uppercase
//hint call the toUpperCase() method on the string
}
}

//question 5
/*
* Generate a random number between 1 and 100.
* format it to be shown with two decimal places.
* If the number is even, print the formatted number. If the number is odd, print "Odd".
*/
public class exercise_5 {
public static void main(String[] args) {
double random = 0;
for (int i = 0; i < 100; i++) {
//generate a random number between 1 and 100 and assign it to the variable random
// hint: use Math.random() to generate a random number between 0 and 1
//format the number to two decimal places
random = Math.random() * 100;
// Change the type of the random to integer
int randomInt = (int) random;
//if the number is even, print the formatted number.
//If the number is odd, print "Odd".
if (randomInt % 2 == 0) {
//print the formatted number
System.out.println(randomInt);
} else {
//print "Odd"
System.out.println("Odd");
}
}
}
}
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.