Today we went over primitive types and reviewed their uses in a kahoot. We also learned about type casting, Strings, and we introduced the concept of arrays.
Link to the repl.it used today: https://replit.com/@offexe/9-18-2022#Main.java
Homework: Create a program that takes input that represents a student's grade, age, gpa, name, and school. What types of variables will you use for each? Then, print out all the information about that student in one line with whatever formatting you want.
Extra Challenge: Create an array from input of grades a student has in each of their classes, then calculate the gpa with 3 decimal points.
Please send your solutions in the comments below :)
Jerry's work
import java.util.Scanner; public class epiccode { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Please input a year you would like to find out if it was a leap year or not"); int year = input.nextInt(); if (year%4==0) { System.out.println("This year is a leap year"); } else { System.out.println("This is not a leap year"); } } }
System.out.println("How many classes do you have"); int n = sc.nextInt(); System.out.println("Type in your grades:"); float[] arr = new float[n]; float sum = 0.0f; for (int i = 0; i < n; i++) { arr[i] = sc.nextFloat(); sum =sum + arr[i]; } System.out.printf("GPA= %5.3f%n", (sum/n));
import java.util.Scanner; public class PrimitiveTypes { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("What is your grade:"); int grade = sc.nextInt(); System.out.println("What is your age:"); int age = sc.nextInt(); System.out.println("What is your name?"); String name = sc.next(); System.out.println("What is your gps?"); float gpa = sc.nextFloat(); System.out.println("What is your school?"); String school = sc.next(); System.out.println("Here are your information"); System.out.printf("Name:%s Age:%d School:%S Grade:%d GPA:%5.3f%n", name, age, school, grade, gpa); } }