Java Assignment
1. Hello World:
javapublic class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. Variables and Data Types:
javapublic 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:
javapublic 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:
javapublic class LoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration " + i);
}
}
}
5. Arrays:
javapublic 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:
javapublic 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):
javapublic 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:
javapublic 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:
javaimport 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)
javaimport 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:
- public class Exercise { public static int additionFunction(){ int x = 7; return x + y; } }
--> Create a new variable called y 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:
- public class Exercise { public boolean returnTrueExp(){ boolean x = true; boolean y = true; // Add here the operator return x y; } }
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:
- public class Exercise { public String sayHello(){ String str1 = "Hello "; String str2 = "My Friends"; // Write your codes here // use return statement to return the result return ; } }
Join the two strings and return the result using the "return" statement.
5. Given the following code:
- public class Exercise { public boolean checkPositive(){ int x = 99; // Write your conditional Statement Here // Use return true; for returning if the condition is true; } }
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:
- public class Exercise {
- public int daysOfMonth(){
- String monthName = "May";
- int number_Of_DaysInMonth = 0;
- return number_Of_DaysInMonth;
- }
- }
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:
- public class Exercise { public int sumUpNumbers(){ int sum = 0; // Write your loop here: Sum up numbers from 1 to 10 return sum; } }
Write a loop that allows us to sum the numbers from 0 to 10 and stores the result in the "sum" variable.
Comments
Post a Comment