Java Assignments Helpful For Beginners
Java Assignments Helpful For Beginners
Java Assignments Helpful For Beginners
JAVA ASSIGNMENTS
LAB 1-12
TASK 1
In this assignment, you will create a program that computes the distance an object will
fall in Earth’s gravity
class GravityCalculator {
3. Run it in.
What is the output of the unmodified program? Include this as a comment in the source
code of your submission.
Modify the example program to compute the position of an object after falling for 10
seconds, outputting the position in meters. The formula in Math notation is:
2
x(t) = 0.5 × at + vit + xi
t Time (s) 10
Note: The correct value is -490.5 m. Java will output more digits after the decimal place,
but that is unimportant.
CODE:
package gravitycalculator;
double a =-9.81;
double vi = 0.0;
double t = 10.0;
double xi = 0.0;
OUTPUT
TASK 2
OUTPUT
TASK 3
Work with primitive data types in Java. Create new project and add class Formula with
next methods:
Variants
CODE
package parctice3;
double u1 = Math.pow((8+Math.pow(Math.abs(x-y),2)+1),1./3.)/(x * x + y * y +
2);
TASK 4
import java.util.Scanner;
import java.lang.*;
import java.io.IOException;
first.set();
second.set();
switch (operation){
case "+":
result = first.add(second);
break;
case "-":
result = first.subtract(second);
break;
case "*":
result = first.multiply(second);
break;
case "/":
result = first.divide(second);
break;
default:
result.print();
result.setElement(index,value);
result.print();
index = input.nextInt();
System.out.println(" ");
TASK 5
Create projects Employees. In project Employees create class Employee which contain
next private attributes:
Create another class Manager which will be extends class Employee additional field
- double bonus
and method
setBonus().
Methods getSalary() and toString() must be changed, because in Manager they are
distinct.
CODE
package employee;
first.print();
boss.print();
JAVA CLASS:
package employee;
import java.util.Scanner;
public Employee() {
setId(nextid());
setFname(input.nextLine());
setSurname(input.nextLine());
setSalary( input.nextDouble());
System.out.println();
public manager(){
setBonus(input.nextDouble());
@Override
System.out.println(); }
@Override
raiseSalary();
return ttlsalary; }
/*by 10% */
OUTPUT
TASK 6
Use code from previous practice #5. In class Employee add methods:
Check how this method work. Create two different objects and print them with using
methods toString () and them hash code. For checking method equals():
- compare two objects with similar data; compare two objects with different data.
For checking method clone () create object and his copy. Compare objects (they must be
equal). Print objects. In copy change salary. Compare objects. Print objects.
CODE:
import java.util.Scanner;
class employ implements Cloneable {
private static int id = 1234;
private String fname;
private String surname;
private double salary;
Scanner input = new Scanner(System.in);
public static int getId() { return id; }
public String getName() { return fname +" " + surname; }
public double getSalary() { return salary; }
public void setSalary(double p ){ this.salary = p; }
public void setId(int id) { this.id = id; }
public void setFname(String fname) { this.fname = fname; }
public void setSurname (String surname) {this.surname = surname; }
public static int nextid() { return getId() + 1; }
public employ() {
setId(nextid());
System.out.println("ENTER YOUR FIRST_NAME:");
setFname(input.nextLine());
System.out.println("ENTER YOUR SURNAME:");
setSurname(input.nextLine());
System.out.println("ENTER THE AMOUNT OF SALARY: ");
setSalary( input.nextDouble());
}
public void print() {
System.out.println("***EMPLOYEE***\nEMPLOYEE NO " + getId());
System.out.println("FULL NAME :" + getName());
System.out.println("SALARY : " + getSalary());
System.out.println();
}
@Override
public boolean equals(Object obj){
if (obj instanceof employ){
employ copy = (employ)obj;
return (fname.equals(copy.fname) && surname.equals(copy.surname) &&
id==copy.id && salary == copy.salary);}
else{ return false;}
}
@Override
public Object clone(){
try{
employ copycat = (employ)super.clone();
return copycat;}
catch(CloneNotSupportedException e){
throw new InternalError(e.toString()); }
}
@Override
public int hashCode(){
Double replicate = new Double(getSalary());
@Override
public double getSalary() {
raiseSalary();
double ttlsalary = super.getSalary() + getBonus();
return ttlsalary; }
public void raiseSalary() {
/*INCREASE SALARY BY 10% */
setSalary(super.getSalary() + (0.1 * super.getSalary()));
}
}
OUTPUT:
TASK 7
identification number (int id); String name; String surname; String group; array of
subjects (Subject [] subjects)which learned student.
+Create class Utils with static method for calculating average mark for every student
String getAverageMark(Students[] student).
CODE:
import java.util.Scanner;
public class student {
Scanner input = new Scanner(System.in);
private static int id =1234;
private String firstname;
private String surname;
private static String group;
private static Subject[] subjects;
private String avg ;
}//END OF STUDENT
class seven {
OUTPUT:
TASK 8
Variant 1
CODE:
import java.util.Scanner;
public class eight {
public static void main(String[]args){
Scanner input = new Scanner(System.in);
System.out.println("ENTER SENTENCE");
String sentence = input.nextLine();
RESULTS:
TASK 9
Create hierarchy of classes of Vehicles. Super class is abstract class Vehicle With one
abstract method
Every vehicle is individual class, which extends Vehicle and override method move(). For
example:
System.out.println(“Bicycle № ”+ id + “moving”); } }
In method main in loop random create array of different vehicle objects with special id,
besides abstract class Vehicle. Print elements of created array.
CODE:
import java.util.Random;
@Override
@Override
@Override
switch(value){
case 1:
bus01.move(bid);
break;
case 2:
bicycle1.move(cid);
break;
case 3:
roller1.move(rid);
break;
default:
System.out.print("ERROR");
break; } } } }
OUTPUT:
TASK 10
Create abstract class Machine, which implement interface Run with next attributes
(name, weight, max speed, count of passenger). Create class Car and Bus which extends
Machine. Define constructors with corresponding parameters for creating objects and
override methods. Try to create objects of classes Car and Bus and invoke them
methods.
CODE:
import java.util.Random;
interface Run{
void print();
int getmaxspeed();
int getweight();
void move();
}
abstract class machine implements Run{
String name;
int weight, maxSpeed, countOfPassengers;
}
class car extends machine{
@Override
public void print(){ System.out.println("CAR NAME: "+name+"\nMAX SPEED:
"+getmaxspeed()+"\nWEIGHT: "+getweight()+"\nNo OF PASSENGERS:
"+countOfPassengers); }
public int getmaxspeed(){return maxSpeed; }
public int getweight(){return weight; }
public void move(){ System.out.println("car now moving!..");}
OUTPUT:
TASK 11
Create class Person which contain name, surname and patronymic. Implement interface
Comparable by class Person for sequencing objects by surname. Check work of interface
Comparable with using collection TreeMap.
CODE:
import java.util.*;
class person implements Comparable<person>{
@Override
public String toString() {
return "person{" +
"firstname='" + firstname + '\'' +
", surname='" + surname + '\'' +
", patronymic='" + patronymic + '\'' +
'}';
}
@Override
public int compareTo(person p){
return this.getSurname().compareTo(p.getSurname());
}
}
public class eleven {
public static void main(String[] args) {
TreeMap<Integer,person> mtu = new TreeMap<>();
mtu.put(101,new person("sarah","c","sanya"));
mtu.put(102,new person("yusuf","b","suleiman"));
mtu.put(103,new person("intisar","a","alfonzo"));
while(it.hasNext()){
Map.Entry p = (Map.Entry)it.next();
System.out.println( p.getKey()+" - "+p.getValue()); }
}
public static <K,V extends Comparable<V>> TreeMap<K,V> sortbysurname(final
TreeMap<K,V> map){
Comparator<K> com = new Comparator<K>() {
@Override
public int compare(K o1, K o2) {
return map.get(o1).compareTo(map.get(o2)); }
};
TreeMap<K,V> sortedd = new TreeMap<K,V>(com);
sortedd.putAll(map);
return sortedd; } }
OUTPUT:
TASK 12
Using classes from practice #5 create your own classes exception with constructors.
Create class exception FieldLengthLimitException. For example, you can specify the
maximum number of characters for the employee’s first and last name. If you try to
assign a string with a large number of characters to the name field,throw the
FieldLengthLimitException (passing the corresponding text message) to it, which you
catch in the main () method and inform the user about the problem.
Also create an IncorrectSalaryException class. Throw this type of exception when trying
to assign a negative salary. And also catch it in the main () method.
All exceptions defined in the program must be inheritors of the Exception class.
CODE:
import java.util.Scanner;
class worker {
private static int id = 1234;
private String fname;
private String surname;
private double salary;
Scanner input = new Scanner(System.in);
public static int getId() { return id; }
public String getName() { return fname +" " + surname; }
public double getSalary() { return salary; }
public void setSalary(double p ){ this.salary = p; }
public void setId(int id) { this.id = id; }
public void setFname(String fname) { this.fname = fname; }
public void setSurname (String surname) {this.surname = surname; }
public static int nextid() { return getId() + 1; }
public worker() {
boolean flag = true;
setId(nextid());
while (flag) {
try {
System.out.println("ENTER YOUR FIRST_NAME:");
String s = input.nextLine();
if (s.length() > 10) {
throw new FieldLengthLimitException("LENGTH LIMIT 10 : PLEASE
RETRY \n");
} else {setFname(s); flag = false; }
} catch (FieldLengthLimitException e) {System.out.println(e); }
}
flag = true;
while(flag) {
try {
System.out.println("ENTER YOUR SURNAME:");
String s = input.nextLine();
while (flag) {
if (s.length() > 10) {
throw new FieldLengthLimitException("LENGTH LIMIT 10 : PLEASE
RETRY \n");
} else {setSurname(s); flag = false; }
}
} catch (FieldLengthLimitException e) {System.out.println(e); }
}
flag = true;
while(flag) {
try {
System.out.println("ENTER THE AMOUNT OF SALARY: ");
double s = input.nextDouble();
while (flag) {
if (s < 0) {
throw new IncorrectSalary("LESS THAN 0 SALARY ERROR : PLEASE
RETRY \n");
} else {setSalary(s); flag = false; }
}
} catch (IncorrectSalary e) {System.out.println(e); }
}
}
public void print() {
System.out.println("***EMPLOYEE***\nEMPLOYEE NO " + getId());
System.out.println("FULL NAME :" + getName());
System.out.println("SALARY : " + getSalary());
System.out.println();
}
}
class managerr extends worker {
private double bonus;
public void setBonus(double b) { this.bonus = b;}
public double getBonus() { return bonus; }
public managerr(){
System.out.println("ENTER THE BONUS AMOUNT: ");
setBonus(input.nextDouble());
}
@Override
@Override
public double getSalary() {
raiseSalary();
double ttlsalary = super.getSalary() + getBonus();
return ttlsalary; }
public void raiseSalary() {
/*INCREASE SALARY BY 10% */
setSalary(super.getSalary() + (0.1 * super.getSalary()));
}
}