Java Assignment

 1. Hello World:

java
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }

2. Variables and Data Types:

java
public class DataTypesExample { public static void main(String[] args) { int age = 25; double height = 5.8; String name = "John Doe"; System.out.println(name + " is " + age + " years old and is " + height + " feet tall."); } }

3. Conditional Statements:

java
public class ConditionalExample { public static void main(String[] args) { int number = 10; if (number > 0) { System.out.println("The number is positive."); } else { System.out.println("The number is non-positive."); } } }

4. Loops:

java
public class LoopExample { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { System.out.println("Iteration " + i); } } }

5. Arrays:

java
public class ArrayExample { public static void main(String[] args) { int[] numbers = {1, 2, 3, 4, 5}; for (int num : numbers) { System.out.println(num); } } }

6. Functions/Methods:

java
public class MethodExample { public static void main(String[] args) { int result = addNumbers(5, 7); System.out.println("Sum: " + result); } static int addNumbers(int a, int b) { return a + b; } }

7. Object-Oriented Programming (OOP):

java
public class Car { String make; String model; public Car(String make, String model) { this.make = make; this.model = model; } public void displayInfo() { System.out.println("Car: " + make + " " + model); } public static void main(String[] args) { Car myCar = new Car("Toyota", "Camry"); myCar.displayInfo(); } }

8. Exception Handling:

java
public class ExceptionExample { public static void main(String[] args) { try { int result = divide(10, 0); System.out.println("Result: " + result); } catch (ArithmeticException e) { System.out.println("Error: " + e.getMessage()); } } static int divide(int a, int b) { return a / b; } }

9. File I/O:

java
import java.io.File; import java.io.FileWriter; import java.io.IOException; public class FileIOExample { public static void main(String[] args) { try { File file = new File("example.txt"); FileWriter writer = new FileWriter(file); writer.write("Hello, File I/O!"); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }

10. Android Basics (Button Click Event): (Assuming you have an Android project set up in Android Studio)

java
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import androidx.appcompat.app.AppCompatActivity; 
public class MainActivity extends AppCompatActivity
@Override protected void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button clickButton = findViewById(R.id.button);   clickButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Handle button click event System.out.println("Button Clicked!"); } }); } }

Remember to replace R.id.button with the actual ID of the button in your layout file (activity_main.xml).


1. We have the following code:


--> Create a new variable called and assign the value "3" to it


2. 

public class Exercise {

    public boolean playWithBooleans(){

        // Create your boolean variable "b1" here 

        return b1;

    }

}

Create a variable of type boolean and name it as "b1"assign true value to b1.


3. We have 2 booleans in this code:

Your mission is to add a specific operator between x and y, so that this function will return true (according to the boolean expressions)


4. We need to concatenate 2 strings and return the result in our code:

   

Join the two strings and return the result using the "return" statement.


5. Given the following code:


Write a conditional statement, that checks if x is positive or not.

If x is positive, let the function returns true.

Else if x is negative, let the function returns false.

to return a true value, after your conditional statement just add:

return true;


6. Days of a Month

We need to create a conditional statement using a switch to check if "monthName" which is the variable that holds the month name , is a 31-days month or 30-days month.

Given this code:


We need to return the number of days using an integer variable called: "number_Of_DaysInMonth"

it is initialised with zero value.

for example;

if January is 31 , then we assign number_Of_DaysInMonth = 31.

Note: Suppose that February is 28 days.


7. Making Use of Loops

We need to sum the numbers from  1 to 10 using the loops!

Given the following code:


Write a loop that allows us to sum the numbers from 0 to 10 and stores the result in the "sum" variable.




Comments

Popular posts from this blog

Widgets

List View : Planet Application

Recycler View: MarketApp