0% found this document useful (0 votes)
10 views

PA 303.2.1 - Practice Assignment - Core Java - Variables

This document contains 8 Java classes with examples of using variables of different types like int, double, and performing basic math operations. It also shows casting between types and using final variables.

Uploaded by

jewelcsebu045
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

PA 303.2.1 - Practice Assignment - Core Java - Variables

This document contains 8 Java classes with examples of using variables of different types like int, double, and performing basic math operations. It also shows casting between types and using final variables.

Uploaded by

jewelcsebu045
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PA 303.2.

1 - Practice Assignment - Core Java - Variables

package com.perscholas.java_basics;

public class JavaBasicsClass {

public static void main(String[] args){


int num1 = 10;
int num2 = 20;
int sum = num1 + num2;
System.out.println("The sum of " + num1 + " and " + num2
+ " is: " + sum);
}
}

package com.perscholas.java_basics;

public class JavaBasicsClass2 {

public static void main(String[] args){


double num1 = 10.5;
double num2 = 20.3;
double sum = num1 + num2;
System.out.println("The sum of " + num1 + " and " + num2
+ " is: " + sum);
}
}

package com.perscholas.java_basics;

public class JavaBasicsClass3 {

public static void main(String[] args){


int num1 = 10;
double num2 = 20.5;
double sum = num1 + num2;
System.out.println("The sum of " + num1 + " and " + num2
+ " is: " + sum);
}
}

package com.perscholas.java_basics;

public class JavaBasicsClass4 {

public static void main(String[] args){


// int num1 = 20;
// int num2 = 10;
//
//
// int larger = (num1 > num2) ? num1 : num2;
//
// int smaller = (num1 < num2) ? num1 : num2;
//
// int result = larger / smaller;
//
// System.out.println("The result of dividing the larger
number by the smaller number is: " + result);

double num1 = 20.0;


int num2 = 10;

double larger = (num1 > num2) ? num1 : num2;

double smaller = (num1 < num2) ? num1 : num2;

double result = larger / smaller;

System.out.println("The result of dividing the larger


number by the smaller number is: " + result);
}
}

package com.perscholas.java_basics;

public class JavaBasicsClass5 {

public static void main(String[] args){


double num1 = 20.0;
double num2 = 10.5;
double larger = (num1 > num2) ? num1 : num2;
double smaller = (num1 < num2) ? num1 : num2;
double result = larger / smaller;
System.out.println("The result of dividing the larger
number by the smaller number is: " + result);
int result2 = (int) result;
System.out.println("The result after casting to an
integer is: " + result2);
}
}

package com.perscholas.java_basics;

public class JavaBasicsClass6 {

public static void main(String[] args){


int x = 5;
int y = 6;
int q = y / x;

System.out.println("The value of q after assigning y/x to


it: " + q);

q = (int) ((double) y);

System.out.println("The value of q after casting y to a


double and assigning it: " + q);
}
}
package com.perscholas.java_basics;

public class JavaBasicsClass7 {

public static void main(String[] args){


final double PI = 3.14159;
double radius = 5.0;
double area = PI * radius * radius;
System.out.println("The area of the circle with radius "
+ radius + " is: " + area);
}
}

package com.perscholas.java_basics;

import java.text.DecimalFormat;

public class JavaBasicsClass8 {

public static void main(String[] args){


String product1 = "Coffee";
double price1 = 2.50;

String product2 = "Cappuccino";


double price2 = 3.00;

String product3 = "Espresso";


double price3 = 2.00;

int quantity1 = 3;
int quantity2 = 4;
int quantity3 = 2;

double subtotal = (price1 * quantity1) + (price2 *


quantity2) + (price3 * quantity3);

final double SALES_TAX = 0.08;

double totalSale = subtotal * (1 + SALES_TAX);


DecimalFormat df = new DecimalFormat("#.##");
String formattedSubtotal = df.format(subtotal);
String formattedTotalSale = df.format(totalSale);

System.out.println("Your Cafe Order:");


System.out.println(quantity1 + " " + product1 + " at $" +
price1 + " each");
System.out.println(quantity2 + " " + product2 + " at $" +
price2 + " each");
System.out.println(quantity3 + " " + product3 + " at $" +
price3 + " each");
System.out.println("Subtotal: $" + formattedSubtotal);
System.out.println("Total Sale (including 8% sales tax):
$" + formattedTotalSale);
}
}

You might also like