Objects Creating Objects Create the following object literals: Create an object
ID: 3701586 • Letter: O
Question
Objects
Creating Objects
Create the following object literals:
Create an object called car with the properties make(string), model(string),year(number).
Create an object called animal with the properties animalName(string),animalType(string), age(number), neutered(boolean).
Create an object called person with the properties firstName(string),lastName(string), age(number), occupation(string) and a property called addresswith properties: street (string), city(string), state(string) and zip(number) .
Updating Objects
Update the properties of the car object to a Honda Civic 2018. Console log the carobject.
Update the properties of the animal object to Chucho, dog, 3 years old, not neutered. Console log the animal object.
Update the properties of the person with the properties to John Smith, age 33, Musician who lives at 101 Code St, Los Angeles 00404 . Console log the personobject.
Update the properties of the animal object using bracket notation to Fido, dog, 4 years old, neutered. Console log the animal object.
Add a new property using dot notation to the car object called VIN and set the value to N1234. Console log the car object.
Object Reflection
Use the typeof operator to inspect the properties of the accounts object on the code block below:
Console log the result of the typeof operator on the accounts object.
Console log the result of the typeof operator on the property Chase the accountsobject.
Console log the result of the typeof operator on the property number of the BankOfAmerica property.
Console log the result of the typeof operator on the property city of the WellsFargoproperty.
Console log the result of the typeof operator on the property stolen of the Chaseproperty.
Console log the result of the typeof operator on the value of null (????)
Retrieving Objects
Given the object accounts below:
Create a function called printServices. The function should take a parameter bank.This function should use the properties of the parameter bank to console log the services of the accounts by bank. The string should read as "{Account number}: stolen card: {true/false}, credit report: {true/false}" . Invoke the function with the each one of the properties (banks) of the accounts object. As an example, invoking the first property should result in "12345: stolen card: true, credit report: true".
Explanation / Answer
package example;
import java.util.Scanner;
import java.util.Arrays;
public class example{
private Scanner sc=new Scanner(System.in);
public void carUpdate(String model, int year){
//JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = example animal =new animal ();
System.out.println ("To update animal info. Enter animal name");
String animalname=sc.next();
System.out.println ("animal type");
String type=sc.next();
System.out.println ("Age of animal ");
int age=sc.nextInt();
System.out.println ("neutered true/false?");
String neutered=sc.next();
animal.animalUpdate(animalname, type, age, neutered);
"jdbc:mysql://localhost/STUDENTS";
// Database credentials
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{ //STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql = "UPDATE CAR SET model='"+model+"'and year="+year+" where id=1";
stmt.executeUpdate(sql);
}
public void animalUpdate(String animalname, String type, int age, String neutered ){
//JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";
// Database credentials
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{ //STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql = "UPDATE animal SET name='"+animalname +"'and type="+type+" and age="+age+" and neutered ="+neutered+"where id=1";
stmt.executeUpdate(sql);
}
public void personUpdate(String fname, String lname, int age, String occupation ){
//JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";
// Database credentials
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{ //STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql = "UPDATE person SET fname='"+fname +"'and lname="+lname+" and age="+age+" and occupation ="+occupation +"where id=1";
stmt.executeUpdate(sql);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("to update car model. Enter model:");
example car= new example();//creating object
String model=sc.next()
System.out.println ("enter year that has to be updated:");
int year =sc.nextInt();
car. carUpdate(model, year);
example animal =new animal ();
System.out.println ("To update animal info. Enter animal name");
String animalname=sc.next();
System.out.println ("animal type");
String type=sc.next();
System.out.println ("Age of animal ");
int age=sc.nextInt();
System.out.println ("neutered true/false?");
String neutered=sc.next();
animal.animalUpdate(animalname, type, age, neutered);
example person=new animal ();
System.out.println ("To update person info. Enter person first name");
String fname=sc.next();
System.out.println ("To update person info. Enter person last name");
String lname=sc.next();
System.out.println ("Age ");
int age=sc.nextInt();
System.out.println ("occupation ");
String occupation =sc.next();
animal.personUpdate(fname, lname, age, occupation );
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.